Following program creates both data step variables and macro variables.
data test;
month = month(input("&sysdate9",date9.));
year = year(input("&sysdate9",date9.));
call symput('month',compress(put(month,best.)));
call symput('year',compress(put(year,best.)));
run;
%put &year;
%put &month;
And from @ballardw:
And a way to do it without a data step:
%let sysmonth= %sysfunc(month("&sysdate"d)); %let sysyear= %sysfunc(year("&sysdate"d)); %put &sysmonth &sysyear;
Remember that SYSDATE has the start date of the SAS session, not the calendar date at the time of execution.
Following program creates both data step variables and macro variables.
data test;
month = month(input("&sysdate9",date9.));
year = year(input("&sysdate9",date9.));
call symput('month',compress(put(month,best.)));
call symput('year',compress(put(year,best.)));
run;
%put &year;
%put &month;
And from @ballardw:
And a way to do it without a data step:
%let sysmonth= %sysfunc(month("&sysdate"d)); %let sysyear= %sysfunc(year("&sysdate"d)); %put &sysmonth &sysyear;
Remember that SYSDATE has the start date of the SAS session, not the calendar date at the time of execution.
I' d like to get month as Jun for example. Is it possible?
Hi Nick
I have found this very useful, but I recently come across a situation slightly different. The macro returns the below values, but for my code I need it to say "OCT" rather than "10". I have tried formatting but it did not work. Is there a way to amend this code to get it to output the character 3 letter name of the month? Thanks
macro result:
10 2016
format attempt:
proc format;
value $ monthnew '10' ='OCT';
run;
data test;
month = month(input("&sysdate9",date9.));
year = year(input("&sysdate9",date9.));
call symput('month',compress(put(month,monthnew.)));
call symput('year',compress(put(year,best.)));
run;
%put &year;
%put &month;
I'd like to automate a YTD report that pulls data from January to December every year. There is always a 1-month lag.
Run date date parameter for data
December 2016 January to November 2016
January 2017 January to December 2016
February 2017 January to January 2017
Any code you can share is greatly appreciated.
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.
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.