SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

Im trying to have 5 macro variables,
%tday which contains todays full date
%yyyy which contains todays year as 4 digits
%yyyy2 which contains last months year as 4 digits
%mm which contains todays month as 2 digits
%mm2 which contains last months month as 2 digits.
for eg todays(28/07/2008) would be
tday=28072008
yyyy=2008
yyyy2=2008
mm=07
mm2=06
But if for eg todays date was 15/01/2009 is where i get my problem!

Please help

Thanks
Spud
4 REPLIES 4
Olivier
Pyrite | Level 9
Hi Spud.
Instead of hard coding today's date and substracting from it, you can let SAS do computations. You can do that with the %SYSFUNC macro function, but it becomes a little boring when you have more than you function to access at a time. So what I think is the most simple solution is to create dates in a Data step, and then record their values, using the Call Symput routine. You can use functions as INTNX to build last month's date, and formats to extract parts of the information.
[pre]
DATA _NULL_ ;
today = TODAY() ;
lastMonth = INTNX("MONTH", today, -1) ;
CALL SYMPUT("tday", PUT(today, DDMMYYN8.)) ;
CALL SYMPUT("yyyy", PUT(today, YEAR4.)) ;
CALL SYMPUT("yyyy2", PUT(lastMonth, YEAR4.)) ;
CALL SYMPUT("mm", PUT(MONTH(today),Z2.)) ;
CALL SYMPUT("mm2", PUT(MONTH(lastMonth),Z2.)) ;
RUN ;
%PUT _USER_ ;
[/pre]
Regards
Olivier
deleted_user
Not applicable
Thanks Olivier

It worked a treat!
deleted_user
Not applicable
Last question if i want a 3 letter month what would be the format?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You have a choice, either use the SAS FORMAT MONNAME or WORDDATE with the trailing length specification to truncate the displayed value. If you need the string in uppercase (as I do often), assign a SAS character variable and use the UPCASE function and the PUT function with the desired format.

Scott Barry
SBBWorks, Inc.
___________________

SAS 913 DOC - MONNAME format (with examples!):

http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000201049.htm


SAS 913 DOC - FORMATS chapter in SAS Language Reference: Dictionary

http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000309859.htm

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 4 replies
  • 5149 views
  • 0 likes
  • 3 in conversation