I have the following piece of code. It works.
DATA T_GROUP1
T_GROUP2
T_GROUP3
;
SET T_GROUP ;
IF GROUP_COUNT = 1 THEN DO ;
CURRENT = INT(TIME()*100);
RANDOM = INT(RANUNI(CURRENT)*100000);
OUTPUT T_GROUP1;
END ;
ELSE
IF GROUP_COUNT = 2 THEN DO ;
CURRENT = INT(TIME()*100);
RANDOM = INT(RANUNI(CURRENT)*100000);
OUTPUT T_GROUP2;
END ;
ELSE
IF GROUP_COUNT = 3 THEN DO ;
CURRENT = INT(TIME()*100);
RANDOM = INT(RANUNI(CURRENT)*100000);
OUTPUT T_GROUP3;
END ;
RETURN ;
PROC SORT DATA=T_GROUP1 ; BY RANDOM ;
PROC SORT DATA=T_GROUP2 ; BY RANDOM ;
PROC SORT DATA=T_GROUP3 ; BY RANDOM ;
T_GROUP has a variable called MAXNUM that captured the maximum number of GROUP_COUNT, in this case, MAXNUM=3.
Suppose I would like to make the above ‘repeatable’ code into a DO LOOP, I think I might need something like DO I = 1 TO 3 THEN DO;
Suppose my input data are not always the same, sometimes could have more and sometimes could have less number of GROUP_COUNT and I would like to maintain the above code structure to reflect that. Looks like I really would like to make MAXNUM into some kind of Global variables and as well a macro to call the whole thing. How can I do it??
Check this solution
%MACRO TGRUP;
DATA _NULL_;
SET T_GROUP;
CALL SYMPUT('MAXN', MAXNUM);
RUN;
%DO I=1 %TO &MAXN;
DATA T_GROUP&I;
SET T_GROUP;
IF GROUP_COUNT= &I THEN DO;
CURRENT = INT(TIME() *100);
RANDOM = INT(RANUNI(CURRENT)*100000);
OUTPUT T_GROUP&I;
END;
RUN;
PROC SORT DATA = T_GROUP&I; BY RANDOM;
%END;
%MEND TGRUP;
%TGRUP;
Hope this helps....!
Thanks
Dhanasekaran R
Here's a slight improvement to the previous (and well done) solution. It's more efficient to read the starting table once to create all the requested datasets.
%MACRO TGRUP;
DATA _NULL_;
SET T_GROUP;
CALL SYMPUT('MAXN', MAXNUM);
STOP;
RUN;
DATA
%DO I=1 %TO &MAXN;
T_GROUP&I
%end;
;
SET T_GROUP;
%DO I=1 %TO &MAXN;
IF GROUP_COUNT= &I THEN DO;
CURRENT = INT(TIME() *100);
RANDOM = INT(RANUNI(CURRENT)*100000);
OUTPUT T_GROUP&I;
END;
%end;
RUN;
%DO I=1 %TO &MAXN;
PROC SORT DATA = T_GROUP&I; BY RANDOM;
RUN;
%end;
%MEND TGRUP;
%TGRUP;
Thank you Dhanasekaran R and BobD very much!
Your thoughtful hard work and expertise are deeply appreciated!
Now not only this piece of code works, I managed to ‘mimic’ this Macro methodology / syntax to apply it to other part of my code before I set up some more SQL... Now I basically have attained the kind of result I expected to see; probably I would still like to do a bit more of testing before I really say ‘it’s finished!’
Thanks, buddy!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.