DATA Step, Macro, Functions and more

reassign macro variable at first instance of by variable

Reply
Frequent Contributor
Frequent Contributor
Posts: 134

reassign macro variable at first instance of by variable

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.

 

Super User
Super User
Posts: 7,039

Re: reassign macro variable at first instance of by variable

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;

 

Frequent Contributor
Frequent Contributor
Posts: 134

Re: reassign macro variable at first instance of by variable

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.

 

Ask a Question
Discussion stats
  • 2 replies
  • 130 views
  • 0 likes
  • 2 in conversation