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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.