BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

In my below pgm I want to know the difference between duplicate 1 macro and duplicate 2 macro. I am able to produce macro variables with values in  first macro but not second. Please help me understand. Thank you.

data sdtmall1;
input memname $ name $;
datalines;
AE AESEQ
BS BSSEQ
CM CMSEQ
;
%macro duplicates1;
data one;
    do i=1 to 3;
         set sdtmall1;

         call symputx(cats('in',i),memname);
         call symputx(cats('seq',i),name);
		
     end;
run;%mend;
%duplicates1;
%put &in1;
%put &in2;
%put &in3;

%macro duplicates2;
data one;
    %do i=1 %to 3;
         set sdtmall1;

         call symputx(cats('inn',&i.),memname);
         call symputx(cats('seqq',&i.),name);
		
     %end;
run;%mend;
%duplicates2;
%put &inn1;
%put &inn2;
%put &inn3;
4 REPLIES 4
Astounding
PROC Star

The first macro works, but it is somewhat lucky that it does so.  There are no %local macro variables at the time the program runs CALL SYMPUTX.  Therefore CALL SYMPUTX creates macro variables in the global macro environment, so that they still exist when the macro is over.  It would be safer to add a third parameter to CALL SYMPUTX to make sure that (in more complex applications) the macro variables created will exist in the global environment.

 

The second macro illustrates that the author is not well schooled in either SAS language or macro language.  Because &I exists in the local environment, CALL SYMPUTX creates macro variables in the local environment.  By the time the final %PUT statements execute, the local environment (and the macro variables created) have vanished.  You can illustrate this (as well as additional issues involved that are getting the wrong values assigned) by moving the final %PUT statements inside the macro, between the RUN and %MEND statements.

Kurt_Bremser
Super User

The macro preprocessor deals with program code, the data step deals with data. While the datastep do loop repeats the same data step statements, the %do loop repeatedly creates separate data step statements. One set in a do loop works different than three separate set statements created by the %do.

MadhuKorni
Quartz | Level 8
data one;
set sdtmall1;
retain count 0;
count = count + 1;
call symputx(cats('in',count),memname);
call symputx(cats('seq',count),name);
run;

%put &in1 &in2 &in3;
Tom
Super User Tom
Super User

Turn on the MPRINT option to see the different SAS code that the two macros generate.

 

Also make sure to define the macro variables INN1,INN2, etc before calling the second macro so that they will be available after the macro ends.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1274 views
  • 4 likes
  • 5 in conversation