BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GraceStehlin98
Calcite | Level 5

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 !!!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
GraceStehlin98
Calcite | Level 5
thanks !
Kurt_Bremser
Super User

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.

GraceStehlin98
Calcite | Level 5

that works ! thank you !

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 927 views
  • 2 likes
  • 3 in conversation