Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Very new to sas, need to perform export procedure for many datasets called data1, data2, data3 ... data10. Is there a way to operationalize this? I've tried the following without success

LIBNAME input '/home/.../input';

LIBNAME output '/home/.../output';

%macro anynumber(number);

proc export data=input.data&number file="/home/.../output/data&number..dta" dbms=dta replace;
run;    
    
%mend;


do i = 1 to 10;

   %anynumber(i)

end;

run;
question from:https://stackoverflow.com/questions/65852014/running-sas-program-for-multiple-parameters-in-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
884 views
Welcome To Ask or Share your Answers For Others

1 Answer

CALL EXECUTE is the recommended option but a macro loop is also a good option here. You could loop it within the same macro as well. All options are illustrated below.

Usually when I see this type of coding though, my first thought is that someone isn't familiar with BY group processing.

data demo;
   do i=1 to 10;
        *make sure this string matches macro call;
        str = catt('%anynumber(', i, ');');
        *write records to log to check;
        put str;
        call execute(str);
   end;
run;

Another option is a macro loop itself.

%macro loop_export(numLoops=10);

  %do i=1 %to &numLoops;
      %anywhere(&i);
   %end;

%mend;

%loop_export(numLoops=10);

Putting them together:

%macro anynumber(number);
%do i=1 %to &number;
    proc export data=input.data&number file="/home/.../output/data&i..dta" dbms=dta 
   replace;
   run;  
 %end;  
    
%mend;

*will export all 10;
%anyNumber(10);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...