DATA Step, Macro, Functions and more

Macro Variables using the today() statement

Reply
N/A
Posts: 0

Macro Variables using the today() statement

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
Super Contributor
Posts: 260

Re: Macro Variables using the today() statement

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Macro Variables using the today() statement

Thanks Olivier

It worked a treat!
N/A
Posts: 0

Re: Macro Variables using the today() statement

Posted in reply to deleted_user
Last question if i want a 3 letter month what would be the format?
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro Variables using the today() statement

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 4 replies
  • 443 views
  • 0 likes
  • 3 in conversation