BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
atnk
Fluorite | Level 6

Hi, In the data set below, I need to sum the last 3 records of (target) to create the var (sumlast3). The var hit is simply if sumlast3 equals 1. I used the lag to create sumlast3 but it is not working. sumlast3=lag(target)+lag2(target)+lag3(target);

 

obstarget
a1
a0
a1
a1
a1
a1
b0
b1
b1
b1
b1

 

Want:

obstargetsumlast3hit
a1  
a0  
a120
a120
a131
a131
b0  
b1  
b120
b131
b131

 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Your WANT dataset seems the show that you want

sumlast3=target+lag(target)+lag2(target)

which includes the current observation and the prior 2 obs.

 

But you report using 

sumlast3=lag(target)+lag2(target)+lag3(target);

which excludes the current observation and includes the prior three.

 

Also, since you want to reset sumlast3 to missing for the first two observations for any given OBS value, you'll probably want something like:

sumlast3=sum(target,lag(target),lag2(target));
if lag2(obs)^=obs then sumlast3=.;

However, you haven't explained the rules for calculating HIT.

 

I use the SUM function instead of the "+" operators to avoid error messages for the first two observations of the dataset  (the lagged values would be missing).


--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

Your WANT dataset seems the show that you want

sumlast3=target+lag(target)+lag2(target)

which includes the current observation and the prior 2 obs.

 

But you report using 

sumlast3=lag(target)+lag2(target)+lag3(target);

which excludes the current observation and includes the prior three.

 

Also, since you want to reset sumlast3 to missing for the first two observations for any given OBS value, you'll probably want something like:

sumlast3=sum(target,lag(target),lag2(target));
if lag2(obs)^=obs then sumlast3=.;

However, you haven't explained the rules for calculating HIT.

 

I use the SUM function instead of the "+" operators to avoid error messages for the first two observations of the dataset  (the lagged values would be missing).


--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
atnk
Fluorite | Level 6
Thanks, it’s a typo, the hit equals 1 when sumlast3 equals 3. The lag with the sum function works well.
Kurt_Bremser
Super User

In your want data, sumlast3 never equals 1, so hit should never be set to 1.

 


@atnk wrote:

Hi, In the data set below, I need to sum the last 3 records of (target) to create the var (sumlast3). The var hit is simply if sumlast3 equals 1. I used the lag to create sumlast3 but it is not working. sumlast3=lag(target)+lag2(target)+lag3(target);

 

obs target
a 1
a 0
a 1
a 1
a 1
a 1
b 0
b 1
b 1
b 1
b 1

 

Want:

obs target sumlast3 hit
a 1    
a 0    
a 1 2 0
a 1 2 0
a 1 3 1
a 1 3 1
b 0    
b 1    
b 1 2 0
b 1 3 1
b 1 3 1

 

Thanks. 


 

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input obs $ target;
cards;
a	1
a	0
a	1
a	1
a	1
a	1
b	0
b	1
b	1
b	1
b	1
;

data want;
 set have;
 target1=lag(target);target2=lag2(target);
 if obs=lag(obs) and obs=lag2(obs) then do;
  sumlast3=sum(target,target1,target2);
  hit=ifn(sumlast3=3,1,0);
 end;
 drop target1 target2;
run;
atnk
Fluorite | Level 6
Thanks, this solution also works.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 609 views
  • 0 likes
  • 5 in conversation