DATA Step, Macro, Functions and more

Add date to file - giving JAN1960

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Add date to file - giving JAN1960

Good day,

 

I would like to add a date to a file, however, it should reflect the previous month. Reason is the files for December will be received and ran in January, however, the file name should then have DEC2016 in it.

 

My code works, however, it gives an output of JAN1960 (which is day 0).

 

I cannot seem to find what the issue is. My code below:

 

PROC EXPORT 
DATA = V_B
OUTFILE = "&path\V_B_%sysfunc(putn(%eval(%sysfunc(month(-1))),monyy7)).csv"
dbms= csv replace;
RUN;

Thank you


Accepted Solutions
Solution
‎01-06-2017 05:26 AM
Contributor
Posts: 24

Re: Add date to file - giving JAN1960

Hi.

 

This will sort it for you I think.

 

 * create macro var with desired date, migth be helpful ahead;
%let FDATE=%sysfunc(putn(%sysfunc(intnx(MONTH,%sysfunc(date()),-1)),monyy7.));

PROC EXPORT DATA = V_B
OUTFILE = "&path\V_B_&FDATE..csv" dbms= csv replace; RUN;

 

You need do use the DATE function to retrieve the filesystems date and the INTNX function to subtract one month from it.

 

More on DATE: http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect...

 

More on INTNX: https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p10v3sa3i4...

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post


All Replies
Solution
‎01-06-2017 05:26 AM
Contributor
Posts: 24

Re: Add date to file - giving JAN1960

Hi.

 

This will sort it for you I think.

 

 * create macro var with desired date, migth be helpful ahead;
%let FDATE=%sysfunc(putn(%sysfunc(intnx(MONTH,%sysfunc(date()),-1)),monyy7.));

PROC EXPORT DATA = V_B
OUTFILE = "&path\V_B_&FDATE..csv" dbms= csv replace; RUN;

 

You need do use the DATE function to retrieve the filesystems date and the INTNX function to subtract one month from it.

 

More on DATE: http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect...

 

More on INTNX: https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p10v3sa3i4...

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Respected Advisor
Posts: 3,892

Re: Add date to file - giving JAN1960

The following should work:

"&path\V_B_%sysfunc(intnx(month,%sysfunc(today()),-1,b),monyy7.).csv"

You get a date of 1960 because function month(-1) doesn't work the way you believe it does (look it up in the manual) and returns a value of 12 - which SAS interpretes as the 12th day when the count of days starts  - which is 1/1/1960.

Occasional Contributor
Posts: 5

Re: Add date to file - giving JAN1960

Thank you. Both solutions work.

 

I'm still fairly new at sas, so thank you for helping me :-)

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 168 views
  • 1 like
  • 3 in conversation