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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.