BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi Everyone,

So I want to import both file 201711 and 201710.

I use the macro &month=11.

I want to do something like &year.&month-1 to make it 201710.

Can you help me with it?

(clearly, I can create a new macro variable, %let &month_1=10)

Thank you,

HHC

 

%let year=2017;
%let month=11;
data rate ;
infile "&path\Rate for &year.&month..csv"
dlm=','  dsd truncover firstobs=2;
input ....
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can use the %EVAL() macro function to do integer arithmetic.

%let year=2017;
%let month=11;
%let previous=%eval(&month-1);

But what do you do when month=01?

Use the INTNX() function instead.  First convert YEAR and MONTH into an actual date value. Then you can calculate the previous month.

%let year=2017;
%let month=11;
%let this_month=%sysfunc(mdy(&month,1,&year));
%let previous_month=%sysfunc(intnx(month,&this_month,-1));

You can then use the YYMMN6. format to convert it into the string you need for your filename.

"&path\Rate for %sysfunc(putn(&previous_month,yymmn6)).csv"

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You can use the %EVAL() macro function to do integer arithmetic.

%let year=2017;
%let month=11;
%let previous=%eval(&month-1);

But what do you do when month=01?

Use the INTNX() function instead.  First convert YEAR and MONTH into an actual date value. Then you can calculate the previous month.

%let year=2017;
%let month=11;
%let this_month=%sysfunc(mdy(&month,1,&year));
%let previous_month=%sysfunc(intnx(month,&this_month,-1));

You can then use the YYMMN6. format to convert it into the string you need for your filename.

"&path\Rate for %sysfunc(putn(&previous_month,yymmn6)).csv"

 

hhchenfx
Barite | Level 11

Oh Tom,

You are right, I haven't thought bout Jan 🙂

Thanks a lot.

HC

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
  • 2 replies
  • 749 views
  • 0 likes
  • 2 in conversation