BookmarkSubscribeRSS Feed
joel
Calcite | Level 5
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
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
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]
deleted_user
Not applicable
You could use

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

Just a thought.

Ike Message was edited by: WDEisenhauer
joel
Calcite | Level 5
Hello Ike
Yes, it works fine and solves my problem !
Many thanks for your help
Joël
joel
Calcite | Level 5
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1191 views
  • 0 likes
  • 3 in conversation