Add a certain number of days onto a date

Accepted Solution Solved
Reply
Regular Contributor
Posts: 216
Accepted Solution

Add a certain number of days onto a date

Hi

If I have a date variable in '03MAR2010' format and just want to add 60 days onto it, there a way to just add 60? I tried doing something like date + 60 and that did not work--the bolded part below.

Paul

data allKids4;

length AgeRange $ 20;

  set allKids3;

  by entity_id;

format prevdate mmddyy10.;

format permhearingdate mmddyy10.;

format permhearingdateActual mmddyy10.;

if prevdate ne "" and permhearingdate ne "" and (permhearingdate-lag(permhearingdate)) > 213.5

then do;

permhearingdate=lag(permhearingdate) + 213.5;

PermHearingDiff=round((permhearingdate-prevdate)/30.5,1);

end;

run;


Accepted Solutions
Solution
‎05-03-2013 11:01 AM
Contributor
Posts: 23

Re: Add a certain number of days onto a date

Hi Paul,

Sorry for the delay, but I got busy at Global Forum.

Here's a code snippet for you in case you haven't solved your problem:

chkval = LAG(permhearingdate);

IF chkval NE . and chkval GT 213 and permhearingdate NE . THEN

  permhearingdate = chkval + 213;

You mentioned prevdate above, but I wasn't sure if that is an actual variable or the lag of the permhearingdate, so it's not in the IF statement. All SAS keywords are in caps, and I prefer using comparison keywords instead of symbols for comparison operators.

Hope this helps.

View solution in original post


All Replies
Respected Advisor
Posts: 3,124

Re: Add a certain number of days onto a date

Two issues here:

1. you are using lag() conditionally.

2. You are computing self-referentially. 

Solution: Create a temp variable such as Lag_perm=lag(permhearingdate) .

HTH,

Haikuo

Contributor
Posts: 29

Re: Add a certain number of days onto a date

I would do it like this...

data test;
  attrib fromdt length=8. format=DATE9.
         todt   length=8. format=DATE9.
  ;

  fromdt = "03MAR2010"d;

  todt = intnx('DAY',fromdt,60);

run;

Contributor
Posts: 23

Re: Add a certain number of days onto a date

A couple of things in terms of syntax.

if prevdate ne "" and permhearingdate ne "" : Here, you are comparing a SAS date value to a character value. SAS dates are always stored as integers. If you look at the PROC CONTENTS of your dataset "allKids3" and the the variables prevdate and permhearingdate are listed as Type CHAR, then this is the source of your problem, and why you cannot add 60 to the value.

As mentioned before, the LAG() function cannot be used in a conditional statement (IF-THEN, SELECT...), so the bolded code below will not give you the results you are expecting.

if prevdate ne "" and permhearingdate ne "" and (permhearingdate-lag(permhearingdate)) > 213.5 then do;

   permhearingdate=lag(permhearingdate) + 213.5;

   PermHearingDiff=round((permhearingdate-prevdate)/30.5,1);

end;

Can you tell me what you are trying to do here, or provide a few lines of your data? It appears that you want the permhearingdate to be exactly 213 days after the value of the previous record. (the .5 does not do anything for a SAS date - it's the integer number of days since 1/1/1960, so, unlike Excel, .5 does not represent noon of 1/1/1960. What is the PermHearingDiff supposed to represent. I think the solution to your problem is easier than you believe, and I'd like to help you get there.

Regular Contributor
Posts: 216

Re: Add a certain number of days onto a date

Hi Cov_derek

What you indicated is what I am trying to do: "It appears that you want the permhearingdate to be exactly 213 days after the value of the previous record."

Yes, I am trying to add 213 days to the previous record's value of "permhearingdate" and make that value the current record's value of "permhearingdate". This is with the assumption that the current record's value of prevdate and permhearingdate are not null and that the difference the current record's 'permhearingdate' value minus the previous record's 'permhearingdate' value is greater than 213 days.

I was using LAG conditionally due to those stipulations, however did not know it couldn't be used this way.

Paul

Solution
‎05-03-2013 11:01 AM
Contributor
Posts: 23

Re: Add a certain number of days onto a date

Hi Paul,

Sorry for the delay, but I got busy at Global Forum.

Here's a code snippet for you in case you haven't solved your problem:

chkval = LAG(permhearingdate);

IF chkval NE . and chkval GT 213 and permhearingdate NE . THEN

  permhearingdate = chkval + 213;

You mentioned prevdate above, but I wasn't sure if that is an actual variable or the lag of the permhearingdate, so it's not in the IF statement. All SAS keywords are in caps, and I prefer using comparison keywords instead of symbols for comparison operators.

Hope this helps.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 515 views
  • 1 like
  • 4 in conversation