I want to save dataset names into array, then sort the dataset. I need sort over hundred datasets. so I use the do loop.
the macro code doesn't work. can someone help check ? Thanks you !!!
data mbr_cnty_freq;
input
mbr_county:$3.
var_freq
var_name:$30.
;
datalines;
001 928 mbr_county
002 790 mbr_county
003 1500 mbr_county
004 2000 mbr_county
005 29 mbr_county
0065 mbr_county
;
run;
data mbr_cty_freq;
infile datalines dsd;
input
mbr_cty:$15.
var_freq
var_name $20.
;
datalines;
Abilene,259,mbr_cty
Sanford,78,mbr_cty
Chapel Hill,100,mbr_cty
Akron,420,mbr_cty
Durham,70,mbr_cty
Alkol,5,mbr_cty
;
run;
data mbr_ELG_freq;
infile datalines dsd;
input
mbr_elg_cd: $10.
var_freq
var_name:$30.
;
datalines ;
ACN,1420,mbr_elg
MAN,60,mbr_elg
SAA,38,mbr_elg
CBD,70,mbr_elg
AAC,5,mbr_elg
;;;;
run;
%macro sorter;
array varChk{*} $30 mbr_cnty_freq mbr_cty_freq mbr_ELG_freq;
%do i=1 %to dim(varChk) by 1;
proc sort data=varChk(i);
by var_freq;
run;
%end;
%mend sorter;
Thanks !!!
Arrays only work in data steps. They will not work outside of a data step.
To fix the problem, you need to use a different SAS language structure.
%macro sorter;
%let names = mbr_cnty_freq mbr_cty_freq mbr_ELG_freq;
%do i=1 %to %sysfunc(countw(&names));
%let thisname=%scan(&names,&i,%str( ));
proc sort data=&thisname;
by var_freq;
run;
%end;
%mend;
%sorter
Next, it seems like you are not reading in the data properly in the last 2 data sets.
Arrays only work in data steps. They will not work outside of a data step.
To fix the problem, you need to use a different SAS language structure.
%macro sorter;
%let names = mbr_cnty_freq mbr_cty_freq mbr_ELG_freq;
%do i=1 %to %sysfunc(countw(&names));
%let thisname=%scan(&names,&i,%str( ));
proc sort data=&thisname;
by var_freq;
run;
%end;
%mend;
%sorter
Next, it seems like you are not reading in the data properly in the last 2 data sets.
Or you store your dataset names in a dataset, and use call execute from that:
data control;
input memname :$32.;
datalines;
mbr_cnty_freq
mbr_cty_freq
mbr_ELG_freq
;
run;
data _null_;
set control;
call execute("
proc sort data=" !! strip(memname) !! ";
by var_freq;
run;
");
run;
Instead of the control dataset, you may be able to use sashelp.vtable with a proper where condition.
that works ! thank you !
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.