Home > Blockchain >  How do I generate proc steps dynamically?
How do I generate proc steps dynamically?

Time:09-30

I have a number of repetitive lines of code which I want to be able to generate dynamically, the result should look something like this:

PROC SURVEYSELECT DATA=IBIS3_4 (where = (Exkl_UtgUtl_Flyg=3)) METHOD=SRS        SAMPSIZE=&Exkl_UtgUtl_Flyg_kvot SEED=1234567        OUT=Exkl_UtgUtl_Flyg;        
RUN;              

PROC SURVEYSELECT DATA=IBIS3_4 (where = (Exkl_UtgUtl_Tag=3)) METHOD=SRS        SAMPSIZE=&Exkl_UtgUtl_Tag_kvot SEED=1234567        OUT=Exkl_UtgUtl_Tag;        
RUN;

I can do generate the SAS-code quite easily in R. I just define a vector with the relevant strings:

strings<-c("Exkl_UtgUtl_Flyg",
"Exkl_UtgUtl_Tag",
"Exkl_UtgUtl_Farja",
"Exkl_UtgUtl_Taxi",
"Exkl_UtgUtl_Hyrbil",
"Exkl_UtgUtl_Driv",
"Exkl_UtgUtl_Bo",
"Exkl_UtgUtl_Resta",
"Exkl_UtgUtl_Shop",
"Exkl_UtgUtl_Aktiv",
"Exkl_UtgUtl_Annat",
"Exkl_UtgSwe_Flyg",
"Exkl_UtgSwe_Tag",
"Exkl_UtgSwe_Farja",
"Exkl_UtgSwe_Taxi",
"Exkl_UtgSwe_Hyrbil",
"Exkl_UtgSwe_Driv",
"Exkl_UtgSwe_Bo",
"Exkl_UtgSwe_Resta",
"Exkl_UtgSwe_Shop",
"Exkl_UtgSwe_Aktiv",
"Exkl_UtgSwe_Annat")

And then I define a for-loop:

for (i in strings){print(paste0("* ",i,"   *
        
         PROC SURVEYSELECT DATA=IBIS3_4 (where = (",i,"=3)) METHOD=SRS
         SAMPSIZE=&",i,"_kvot SEED=1234567
         OUT=",i,";
         RUN;"))}

I have to copy-paste the output into MS Word and remove all quotes/row numbers/rowbreak-signs, but at least I don't have to program 20 identical lines of code manually.

But is there a way of doing this entirily in SAS? Can I put a do-loop inside a put-statement or something similar, in order to generate the code that I need?

CodePudding user response:

Try if this works. Without your data I cannot test it properly.

You don't need to create a new table for this. If those strings are part of a different table, just call that column in the last part.

data have;
    input strings $40.;
    datalines;
Exkl_UtgUtl_Tag
Exkl_UtgUtl_Farja
Exkl_UtgUtl_Taxi
Exkl_UtgUtl_Hyrbil
;
run;

Create a macro that will spam those proc surveyselect statements.

%macro Survey_select(strings);
    PROC SURVEYSELECT DATA=IBIS3_4 
        (where = (&strings=3)) METHOD=SRS        SAMPSIZE=%sysfunc(cats(&,&strings,_kvot)) SEED=1234567        OUT=&strings
    ;        
    RUN; 
%mend Survey_select;

Call your macro with data step using call execute. If those strings are not in a table of their own, you can easily call them from some other table in this step.

data _null_;
    set have;
    call execute(cats('%Survey_select(',strings,')'));
run;
  •  Tags:  
  • rsas
  • Related