Help using Base SAS procedures

Working on Dates

Occasional Contributor
Posts: 7

Working on Dates

In a dataset, how can I ask the following
I enter 2 dates variables for this year like 01mar08 and 31mar08 being &date1 and &date2
I xant to get same info for last year so I wrote : &date1 - 365
Unfortunately, with 29feb08, I get a wrong date . . .
I suppose I have to interpret it as "Mar-08" minus 12 months but how do I do ?
Many thanks in advance
Posts: 9,368

Re: Working on Dates

That's really strange. When I subtract 365 from the internal value for 29Feb08 (17591) , I get the internal value for 03/01/2007 (17226). Depending on how you're handling and using your macro variables, you may not have the macro variable in the correct numeric form for doing the subtraction. If &DATE1 resolves to the TEXT string 29Feb08, then it's not really a SAS date value -- it's just a text string that happens to look like a date. Your text string has to be recognized as the number of days since Jan 1, 1960 in order for your subtraction to work correctly.

I've inserted some test code below. Your best bet for help is to contact Tech Support so they can look at ALL of your code -- where the macro variables are set and how/where you're performing the subtraction so they can help you with the correct syntax. As you can see in the code below, you would do the subtraction slightly differently in ALL macro versus inside the DATA step program.

** All macro;
%let date1=29Feb08;
%let date2=31Dec08;
%put ------------------***;
%put date1 is: &date1;
%put date2 is: &date2;

%put ------------------***;
%let dateval = %sysfunc(inputn(&date1,date7.));
%let calc = %eval(&dateval - 365);
%put dateval is: &dateval;
%put calc is: &calc;

%let yearago1 = %sysfunc(putn(&calc,mmddyy10.));
%put ******* yearago1 is: &yearago1;

** use in DATA step program;
data testmacvar;
d_date1 = "&date1"d;
d_date2 = "&date2"d;
yearago1 = "&date1"d - 365;
yearago2 = "&date2"d - 365;
format d_date1 d_date2 yearago1 yearago2 mmddyy10.;

options nocenter;
ods listing;
proc print data=testmacvar;

SAS Log & output:
198 %let date1=29Feb08;
199 %let date2=31Dec08;
200 %put ------------------***;
201 %put date1 is: &date1;
date1 is: 29Feb08
202 %put date2 is: &date2;
date2 is: 31Dec08
204 %put ------------------***;
205 %let dateval = %sysfunc(inputn(&date1,date7.));
206 %let calc = %eval(&dateval - 365);
207 %put dateval is: &dateval;
dateval is: 17591
208 %put calc is: &calc;
calc is: 17226
210 %let yearago1 = %sysfunc(putn(&calc,mmddyy10.));
211 %put ******* yearago1 is: &yearago1;
******* yearago1 is: 03/01/2007
213 title; footnote;
214 data testmacvar;
215 d_date1 = "&date1"d;
216 d_date2 = "&date2"d;
217 yearago1 = "&date1"d - 365;
218 yearago2 = "&date2"d - 365;
219 format d_date1 d_date2 yearago1 yearago2 mmddyy10.;
220 run;

NOTE: The data set WORK.TESTMACVAR has 1 observations and 4 variables.

************************ OUTPUT *************************
Obs d_date1 d_date2 yearago1 yearago2

1 02/29/2008 12/31/2008 03/01/2007 01/01/2008

Not applicable
Posts: 0

Re: Working on Dates

You could use


Just a thought.

Ike Message was edited by: WDEisenhauer
Occasional Contributor
Posts: 7

Re: Working on Dates

Posted in reply to deleted_user
Hello Ike
Yes, it works fine and solves my problem !
Many thanks for your help
Occasional Contributor
Posts: 7

Re: Working on Dates

Hello Cynthia
I guess, I wrongly wrote my question
basically, my first date is "01mar08"d and I want to get "01mar07"d or one year ago; in the past I simply did : "01feb08"d - 365 and I got exactly the date last year
After leapday 29feb08 and, as from 01mar08 I get 02mar07 !
I just want to use a way (a function ?) to do it !
Sorry for probably boring question !
But thanks again for help !!!
Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation