BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chuckdee4
Calcite | Level 5
Hello,

I am trying to use the current month and year from %sysdate or any other function within a report.
Would anyone know how to extract these values and assign them to a variable or macr variable.

Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
NickR
Quartz | Level 8

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.

View solution in original post

8 REPLIES 8
NickR
Quartz | Level 8

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.

chuckdee4
Calcite | Level 5
Many thanks Nick will apply this and advice on how it goes.
ballardw
Super User
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.
chuckdee4
Calcite | Level 5
Nick the codes worked just fine, thanks

Ballardw, many thanks for your suggestion seems to do the sae job with less code, will implement it in future projects
Vladimir
Fluorite | Level 6

I' d like to get month as Jun for example. Is it possible?

Dumitru86
Calcite | Level 5

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;

may888
Calcite | Level 5

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.

 

mkeintz
PROC Star
Instead of re-activating a zombie topic, start a new one, using appropriate title.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 57725 views
  • 5 likes
  • 7 in conversation