Hi all,
I wrote a loop to run over each item stored in a macro variable, but the loop is partly working only.
I first write a macro called getdata and then write another macro called call_getdata to do the loop for each value of year and type stored in the macro variables.
If &year = "all", I want to get all observations. Similarly, if &type = "all", all observations are chosen. Otherwise, it only selects the data related to a particular year and type. I have a variable year and indicator in the datain sample containing the values of the years (2018, 2019, 2020, and 2021) and types (typea, typeb, typec)
When the loop goes over specific values of year and type, it's working well. But when it goes over the first value "all", the loop doesn't work.
The error message I received is "Variable all is not on file work.datain".
When I specify option mprint, I saw that the way SAS reads the first %if %then %do %end is that it will select the observation whether year = "all" or type = "all".
Could you please help me to fix it?
%macro getdata(datain,year,type);
/*get data*/
%if &year = "all" %then %do;
data data_out; set &datain; run;
%end;
%else %do;
data data_out; set &datain;
where year = &year;
run;
%end;
%if &type = "all" %then %do;
data datain_out_&type; set data_out; run;
%end;
%else %do;
data data_out_&type; set data_out;
where indicator = "&type";
run;
%end;
%mend;
%macro call_getdata();
%let years = all 2018 2019 2020 2021;
%let types =all typea typeb typec;
%local i j;
%do i=1 %to %sysfunc(countw(&years,%str( )));
%let year = %scan(&years,&i,%str( ));
%do j=1 %to %sysfunc(countw(&types,%str( )));
%let type = %scan(&types,&j,%str( ));
%let year = &year;
%let type = &type;
%getdata(datain=datain,year=&year,type=&type);
%end;
%end;
%mend;
%call_getdata();
Thank you so much in advance for any help.
... View more