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

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
  • 2 replies
  • 871 views
  • 2 likes
  • 2 in conversation