DATA Step, Macro, Functions and more

SAS Dates

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 87
Accepted Solution

SAS Dates

Can anyone please provide help me get right answer

%let date = 2012-11;

%let mydate = %sysfunc(putn(&date, YYMMD7.));

%let me = %sysfunc(intnx(month,&date,-1),yymmd7.);

%put &date;

%put &mydate;

%put &me;

My Answers:

%put &date;

2012-11

%put &mydate;

1965-06

%put &me;

1965-05

I want Answers:

%put &date;

2012-11

%put &mydate;

2012-11

%put &me;

2012-10


Accepted Solutions
Solution
‎03-14-2013 11:12 AM
Super User
Posts: 17,784

Re: SAS Dates

The 2012-11 is in a date format that SAS doesn't recognize (or at least an informat that I coudn't find).

If you remove the hyphen, then it is informat, yymmn6. So I compress out the hyphen, convert it to a date.

You were also referencing the incorrect the macro variable in your last macro variable (me), date rather than mydate.

%let date = 2012-11;

%let mydate = %sysfunc(inputn(%sysfunc(compress(&date, "-")), yymmn6.));

%put &mydate;

%let me = %sysfunc(intnx(month,&mydate,-1),yymmd7.);

%put &me;

View solution in original post


All Replies
SAS Employee
Posts: 12

Re: SAS Dates

There will be many ways to do this, but here is one suggestion.

%let date = 2012-11;

/* we could use in informat or break out the separate date components, as below */

%let y=%substr(%left(&date),1,4);

%let m=%substr(%left(&date),6,2);

%put y=&y m=&m;

/* create a numerical version of the date */

%let mydateN = %sysfunc(mdy(&m.,1,&y.));

/* create a Character (formated) version of the date */

%let mydateF= %sysfunc(putn(&mydateN.,yymmd7.));

/* decrease date and create a new Character (formated) version of the date */

%let me = %sysfunc(intnx(month,&mydateN.,-1),yymmd7.);

%put  &date;

%put MyDate (Numerical) &mydateN;

%put MyDate (Formated) &mydateF;

%put &me;

Super Contributor
Posts: 543

Re: SAS Dates

Hi.

I think  the main problem is in the first assignment of date:

%let date = 2012-11;

While the assumption is that SAS will know what you mean, it is hard without telling SAS that '2012-11' is actually a date, it will not just guess that.

OK, the more straightforward explanation is that SAS will not just assume that the list of characters: 2012-11 means a date. Unless YOU tell it so.

In a %let statement you can have anything: %let date = this is a date, so that means you cannot simply apply a specific date format to a string and expect SAS to know what you mean.

So, again, %let date = 2012-11; translates to a string that does not have the meaning you intended (date). It is just a string of numbers.


So,
%let date = 2012-11; needs to become something like

%let date = '01Nov2012'd;

%put &date;

%let mydate = %sysfunc(putn(&date, yymmd7.));

%put &mydate.;

%let me = %sysfunc(intnx(Month,&date., -1),yymmd7.);

%put &me.;

I hope this clarifies a bit why you are not getting the expected answers.

Good luck,

Anca

Solution
‎03-14-2013 11:12 AM
Super User
Posts: 17,784

Re: SAS Dates

The 2012-11 is in a date format that SAS doesn't recognize (or at least an informat that I coudn't find).

If you remove the hyphen, then it is informat, yymmn6. So I compress out the hyphen, convert it to a date.

You were also referencing the incorrect the macro variable in your last macro variable (me), date rather than mydate.

%let date = 2012-11;

%let mydate = %sysfunc(inputn(%sysfunc(compress(&date, "-")), yymmn6.));

%put &mydate;

%let me = %sysfunc(intnx(month,&mydate,-1),yymmd7.);

%put &me;

Frequent Contributor
Posts: 87

Re: SAS Dates

Thank you all for your feedback. I've a working code now.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 588 views
  • 11 likes
  • 4 in conversation