DATA Step, Macro, Functions and more

Macro with a global variable

Reply
Occasional Contributor
Posts: 9

Macro with a global variable

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

Frequent Contributor
Posts: 75

Re: Macro with a global variable

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

Contributor
Posts: 43

Re: Macro with a global variable

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;

Occasional Contributor
Posts: 9

Re: Macro with a global variable

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!

Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 3 in conversation