turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Working on Dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-17-2008 09:34 AM

Hello

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

Joël

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

Joël

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-17-2008 10:12 AM

Hi:

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.

cynthia

[pre]

** 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.;

run;

options nocenter;

ods listing;

proc print data=testmacvar;

run;

[/pre]

SAS Log & output:

[pre]

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

203

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

209

210 %let yearago1 = %sysfunc(putn(&calc,mmddyy10.));

211 %put ******* yearago1 is: &yearago1;

******* yearago1 is: 03/01/2007

212

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

[/pre]

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.

cynthia

[pre]

** 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.;

run;

options nocenter;

ods listing;

proc print data=testmacvar;

run;

[/pre]

SAS Log & output:

[pre]

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

203

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

209

210 %let yearago1 = %sysfunc(putn(&calc,mmddyy10.));

211 %put ******* yearago1 is: &yearago1;

******* yearago1 is: 03/01/2007

212

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

[/pre]

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-17-2008 10:29 AM

You could use

MDY(MONTH(&date1),DAY(&date1),YEAR(&date1)-1)

Just a thought.

Ike Message was edited by: WDEisenhauer

MDY(MONTH(&date1),DAY(&date1),YEAR(&date1)-1)

Just a thought.

Ike Message was edited by: WDEisenhauer

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-17-2008 10:36 AM

Hello Ike

Yes, it works fine and solves my problem !

Many thanks for your help

Joël

Yes, it works fine and solves my problem !

Many thanks for your help

Joël

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-17-2008 10:30 AM

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 !!!

Joël

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 !!!

Joël