BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Paul_NYS
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
cov_derek
Fluorite | Level 6

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

5 REPLIES 5
Haikuo
Onyx | Level 15

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

TimArm
Obsidian | Level 7

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;

cov_derek
Fluorite | Level 6

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.

Paul_NYS
Obsidian | Level 7

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

cov_derek
Fluorite | Level 6

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 22511 views
  • 1 like
  • 4 in conversation