BookmarkSubscribeRSS Feed
willow2010
Calcite | Level 5

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

3 REPLIES 3
dhana
Fluorite | Level 6

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

BobD
Fluorite | Level 6

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;

willow2010
Calcite | Level 5

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 768 views
  • 0 likes
  • 3 in conversation