SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have a data set called have with 3 columns: FromDate ,UntilDate, YYMM

I want to look at the last row of data set have and put the value of column YYMM into a macro varaible called lastMonth.

What is the way to do it please via call symputx?

 

Data _NULL_;

SET have(How to chose last row);

CALL SYMPUTX('lastmonth',YYMM);

Run;

 

Data have;
format  fromDate  UntilDate  date9.;
INPUT fromDate :date9.  UntilDate :date9. YYMM;
cards;
01JAN2021 21JAN2021  2011
22JAN2021 19FEB2021 2012
20FEB2021 17MAR2021 2102
18MAR2021 28APR2021 2105
;
Run;
3 REPLIES 3
Ronein
Meteorite | Level 14
data _NULL_;
set  have nobs=nobs;
if  _n_=nobs ;
CALL SUMPUTX('lastmonth',YYMM);
Run;

The questions about this code:

Will IF _n_=obs condition done first and after CALL SYMPUTX?

 

ballardw
Super User

@Ronein wrote:
data _NULL_;
set  have nobs=nobs;
if  _n_=nobs ;
CALL SUMPUTX('lastmonth',YYMM);
Run;

The questions about this code:

Will IF _n_=obs condition done first and after CALL SYMPUTX?

 


Make sure to spell SYMPUTX in code correctly. SUMPUTX does not exist.

 

With that IF in the code there are no values at all in the data vector past the If statement until _n_ is equal to nobs.

Example:

data _NULL_;
   set  sashelp.class nobs=nobs;
   if  _n_=nobs ;
   put _n_= name=;
   CALL SYMPUTX('lastname',name);
Run;

%put &lastname.;

Run the code and you can see that the put basically only executes once.

 

 

Lazy code, which you won't see much performance impact from unless you have a moderate number of records, is to just skip any condition at all. The Call symputx would execute for each record and overwrite the previous value of the macro variable and be left with the last value by default.

You can test that with this example:

data _NULL_;
   set  sashelp.class nobs=nobs;
   CALL SYMPUTX('lastname',name);
Run;

%put &lastname.;

If you have a very large data set (millions of records) it may be more efficient to query the dictionary.table or sashelp.vtable for the value of NOBS, stuff that into a macro variable use that as the FIRSTOBS= data set option so only the last record is even pulled into the data step.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1516 views
  • 14 likes
  • 3 in conversation