DATA Step, Macro, Functions and more

previous month

Reply
Super Contributor
Posts: 673

previous month

data dat;

file_date=substr(put("&sysdate"d,mmddyy10.),7,4)||'-'||substr(put("&sysdate"d,mmddyy10.),1,2);
run;

File_date resolves to 2009-06.

How to get 2009-05 instead?
Super Contributor
Super Contributor
Posts: 3,174

Re: previous month

I suggest working with the SAS DATE numeric-type variables for a more straightforward technique, and also so you can use INTNX, for deriving date values and date-strings.

In a DATA step, use the PUT function with a SAS DATE type variable, if you need to assign a character-type variable, for some purpose:

data _null_;
mytoday = "&sysdate9"d;
format mytoday date9. ;
today_yymmdd = put( mytoday,yymmdd6.);
putlog _all_;
run;

The SAS support http://support.sas.com/ website provides SAS-hosted documentation and supplemental technica/conference papers on this topic - use the website SEARCH or a Google advanced search, adding the site:sas.com parmeter to limit your search to the SAS.COM site.

Scott Barry
SBBWorks, Inc.

SAS DOC - Working with Dates in the SAS System:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm


Using SAS® Dates and Times – A Tutorial
Jonas V. Bilenas, JP Morgan Chase, Wilmington, DE
http://www2.sas.com/proceedings/forum2007/226-2007.pdf


TS-668
SAS Dates, Times, and Interval Functions
Definitions and Explanations
SAS Dates, Times, and Datetimes:
http://support.sas.com/techsup/technote/ts668.pdf
N/A
Posts: 0

Re: previous month

Hello SASPhile,

This could be the solution to your problem:

data dat;
length file_date $7;
file_date=put(intnx('month',date(),-1),YYMMD7.);
put file_Date=;
run;

Could you tell whether this solves the problem?

Regards,

Yoba
Valued Guide
Posts: 2,177

Re: previous month

Posted in reply to deleted_user
and the equivalent code to operate on macro variables, is:
%let last_month = %sysfunc( intnx( month, "&sysdate9"d, -1 ), yymmd7 );
%put &last_month ;

(works for me)
Ask a Question
Discussion stats
  • 3 replies
  • 203 views
  • 0 likes
  • 4 in conversation