Question

My goal is to create a SAS stored process is to return data for a single dataset and to filter the columns in that dataset based on a multi-value input parameter passed into the stored process.

Is there a simple way to do this?

Is there a way to do this at all?

Here's what I have so far. I'm using a macro to dynamically generate the KEEP statement to define which columns to return. I've defined macro variables at the top to mimic what gets passed into the stored process when called through SAS BI Web Services, so unfortunately those have to remain as they are. That's why I've tried to use the VVALUEX method to turn the column name strings into variable names.

Note - I'm new to SAS

libname fetchlib meta library="lib01" metaserver="123.12.123.123"
password="password" port=1234
repname="myRepo" user="myUserName";

/* This data represents input parameters to stored process and 
 * is removed in the actual stored process*/
%let inccol0=3;
%let inccol='STREET';
%let inccol1='STREET';
%let inccol2='ADDRESS';
%let inccol3='POSTAL';
%let inccol_count=3;

%macro keepInputColumns;
    %if &INCCOL_COUNT = 1 %then
        &inccol;
    %else
        %do k=1 %to (&INCCOL_COUNT);
             var&k = VVALUEX(&&inccol&k);
        %end;
        KEEP
        %do k=1 %to (&INCCOL_COUNT);
             var&k
        %end;
        ;
%mend;

data test1;
    SET fetchlib.Table1;
    %keepInputColumns;
run;

/*I switch this output to _WEBOUT in the actual stored process*/
proc json out='C:\Logs\Log1.txt';
    options firstobs=1 obs=10;
   export test1 /nosastags;
run;

There are some problems with this. The ouput uses var1, var2 and var3 as the column names and not the actual column names. It also doesn't filter by any columns when I change the output to _webout and run it using BI Web Services.

Was it helpful?

Solution 2

It turns out that the simplest way to implement this was to change the way that the columns (aka SAS variables) were passed into the stored process. Although Joe's answer was helpful, I ended up solving the problem by passing in the columns to the keep statement as a space-separated column list, which greatly simplified the SAS code because I didn't have to deal with a dynamic list of columns.

libname fetchlib meta library="lib01" metaserver="123.12.123.123"
password="password" port=1234
repname="myRepo" user="myUserName";"&repository" user="&user";

proc json out=_webout;
   export fetchlib.&tablename(keep=&columns) /nosastags;
run;

Where &columns gets set to something like this:

Column1 Column2 Column3

OTHER TIPS

OK, I think I have some understanding of what you're doing here.

You can use KEEP and RENAME in conjunction to get your variable names back.

KEEP
        %do k=1 %to (&INCCOL_COUNT);
             var&k
        %end;
        ;

This has an equivalent

RENAME
        %do k=1 %to (&INCCOL_COUNT);
             var&k = &&inccol&k.
        %end;
        ;

and now, as long as the user doesn't separately keep the original variables, you're okay. (If they do, then you will get a conflict and an error).

If this way doesn't work for your needs, and I don't have a solution for the _webout as I don't have a server to play with, you might consider trying this in a slightly different way.

proc format;
  value agef
  11-13 = '11-13'
  14-16 = '14-16';
quit;

ods output report=mydata(drop=_BREAK_);
proc report data=sashelp.class nowd;
format age agef.;
columns name age;
run;
ods output close;

The first part is just a proc format to show that this grabs the formatted value not the underlying value. (I assume that's desired, as if it's not this is a LOT easier.)

Now you have the data in a dataset a bit more conveniently, I think, and can put it out to JSON however you want. In your example you'd do something like

ods output report=work.mydata(drop=_BREAK_);
proc report data=fetchlib.Table1 nowd;
columns 

    %do k=1 %to (&INCCOL_COUNT);
       &&inccol&k.;
    %end;
;
run;
ods output close;

And then you can send that dataset to JSON or whatever. It's actually possible that you might be able to go more directly than that even, but I don't know almost anything about PROC JSON.

Reading more about JSON, you may actually have an easier way to do this.

On the export line, you have the various format options. So, assuming we have a dataset that is just a subset of the original:

proc json out='C:\Logs\Log1.txt';
    options firstobs=1 obs=10;
   export fetchlib.Table1
   (
    %do k=1 %to (&INCCOL_COUNT);
               &&inccol&k.;
            %end;
    )

  / nosastags FMTCHARACTER  FMTDATETIME  FMTNUMERIC ;
run;

This method doesn't allow for the variable order to be changed; if you need that, you can use an intermediate dataset:

data intermediate/view=intermediate;
set fetchlib.Table1;
retain
       %do k=1 %to (&INCCOL_COUNT);
           &&inccol&k.;
        %end;
;
keep
       %do k=1 %to (&INCCOL_COUNT);
           &&inccol&k.;
        %end;
 ;
run;

and then write that out. I'm just guessing that you can use a view in this context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top