BookmarkSubscribeRSS Feed
Almoha
Calcite | Level 5

Hi All

 

 


data have ;
input id date1 res date2;
cards;
111 10AUG2017 10 10AUG2017
111 10SEP2017 20 10SEP2017
111 11SEP2017 30 11SEP2017
111 09MAR2018 18 09MAR2018
;
run;

 

 

i am trying to calculate  the mean of res that 6 months(180 days)  prior to each date1 from date2

 

ID      date1        res   date2           mean
111 10AUG2017 10   10AUG2017     .
111 10SEP2017 20    10SEP2017    10
111 11SEP2017 30    11SEP2017     15
111 09MAR2018 18   09MAR2018    25

 

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data have ;

input id date1 : date9. res date2 : date9.;

format date1 date2 date9.;

cards;

111 10AUG2017 10 10AUG2017

111 10SEP2017 20 10SEP2017

111 11SEP2017 30 11SEP2017

111 09MAR2018 18 09MAR2018

;

run;

 

 

data want;

do _n_=1 by 1 until(last.id);

set have;

by id;

k=lag1(res);

k2=lag2(res);

if _n_=1 then do;

output;

continue;

end;

else if _n_=2 then mean=k;

else mean=sum(k,k2)/2;

output;

end;

drop k:;

run;

 

Regards,

Naveen Srinivasan

Ksharp
Super User
data have ;
input id date1 : date9. res date2 : date9.;
format date1 date2 date9.;
cards;
111 10AUG2017 10 10AUG2017
111 10SEP2017 20 10SEP2017
111 11SEP2017 30 11SEP2017
111 09MAR2018 18 09MAR2018
;
run;
proc sql;
select *,(select mean(res) from have where id=a.id and
   intnx('month',a.date1,-6,'s') < date1< a.date1 )
 from have as a ;
quit;
MarkWik
Quartz | Level 8

Hi @Ksharp Can you please explain the compund expression:

 

 intnx('month',a.date1,-6,'s') < date1< a.date1 

I am keen to understand how that works with three operands. Thank you!

 

Mark 

Ksharp
Super User

pick up the data from HAVE 

where  date1 less than the current record's date1(a.date1)

    and date1 greater than ( the current record's date1 - 6 month ).

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!

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
  • 4 replies
  • 741 views
  • 0 likes
  • 4 in conversation