BookmarkSubscribeRSS Feed
Beto16
Obsidian | Level 7
Hi I have a table that looks like this

Machine. Date. Load. Use
A1. 02/03/16. 160. 31
A1. 02/18/16. 40. 83
A1. 02\25/17. 80. 0
The first row is perfect. What I need is is 2nd row amt 83 is greater than the load of 40 than use the load amt of first row 160.... if it's less than use 40...
Here is what I need

Machine. Date. Load. Use
A1. 02/03/16. 160. 31
A1. 02/18/16. 160 83
A1. 02\25/17. 80. 0

this is what I get
Machine. Date. Load. Use
A1. 02/03/16. 160. 31
A1. 02/18/16. 160 31
A1. 02\25/17. 40 0


Data table_lag;
Set table;
By machine date;
Load_Lag=lag (load);
Use_lag=lag (use);
If first.machine
then

Do;
Load_Lag=lag load;
Use_lag=lag use;
End;
Run;



2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @Beto16,

 

Would this work for you?

data have;
input Machine $ Date :mmddyy. Load Use;
format Date mmddyy8.;
cards;
A1 02/03/16 160 31
A1 02/18/16 40 83
A1 02/25/17 80 0
A2 02/03/16 150 80
A2 02/18/16 70 70
A2 02/25/17 42 43
;

data want;
set have;
by machine date;
if first.machine then load1=load;
else if use>load>. then load=load1;
retain load1;
drop load1;
run;

This code doesn't use a DO statement, but unlike your suggested code it involves a condition with "greater than."

 

It's good that you posted an example, but it would be even better if, in addition, you stated the rule ("if ... greater than ..., then ...") in general terms (e.g. "... then use the load value of the first observation of the machine" or "... then use the load value from the previous observation of the same machine" ...).

 

Edit: Modified the test data for machine A2 a bit to make it more significant.

Beto16
Obsidian | Level 7
Thank you ..I will test it

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1544 views
  • 2 likes
  • 2 in conversation