BookmarkSubscribeRSS Feed
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

The "n" below is not changing at the start of each new ID.  Can macro variables like this be overwritten with each new instance of a by variable?

 

data new ;

set have ;

by id ;

retain n ;

if first.id = 1 then do;
call symputx('lastlot',trunclot) ;
n=&lastlot. ;
end;


DO i = 1 TO n ;

 

etc.

 

2 REPLIES 2
Tom
Super User Tom
Super User

Why would it change?  When SAS compiles the data step it will use the current value of the macro variable. So if it was defined as 5 before SAS started to compile the data step then the code would look like:

%let lastlot=5 ;

data new ;
  set have ;
  by id ;
  retain n ;
  if first.id = 1 then do;
    call symputx('lastlot',trunclot) ;
    n=5 ;
  end;
  DO i = 1 TO n ;
      ...
  end;
run;

 

It looks like you want to do:

data new ;
  set have ;
  by id ;
  retain n ;
  if first.id then n = trunclot ;
  DO i = 1 TO n ;
      ...
  end;
run;

 

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

Yes the second is what I want. I have a complicated piece of code to work out so I was over complicating this simple task. Thanks for your help.

 

sas-innovate-2024.png

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.

 

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
  • 2 replies
  • 667 views
  • 0 likes
  • 2 in conversation