DATA Step, Macro, Functions and more

Extracting month and year from %sysdate

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Extracting month and year from %sysdate

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

Accepted Solutions
Solution
a week ago
Frequent Contributor
Posts: 81

Re: Extracting month and year from %sysdate

[ Edited ]

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


All Replies
Solution
a week ago
Frequent Contributor
Posts: 81

Re: Extracting month and year from %sysdate

[ Edited ]

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.

Occasional Contributor
Posts: 10

Re: Extracting month and year from %sysdate

Many thanks Nick will apply this and advice on how it goes.
Super User
Posts: 10,550

Re: Extracting month and year from %sysdate

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.
Occasional Contributor
Posts: 10

Re: Extracting month and year from %sysdate

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
New Contributor
Posts: 3

Re: Extracting month and year from %sysdate

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

Senior User
Posts: 1

Re: Extracting month and year from %sysdate

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;

Regular Learner
Posts: 1

Re: Extracting month and year from %sysdate

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.

 

Valued Guide
Posts: 797

Re: Extracting month and year from %sysdate

Instead of re-activating a zombie topic, start a new one, using appropriate title.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 13832 views
  • 0 likes
  • 7 in conversation