DATA Step, Macro, Functions and more

How to use Do statement ?

Reply
Frequent Contributor
Posts: 80

How to use Do statement ?

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;



Trusted Advisor
Posts: 1,115

Re: How to use Do statement ?

[ Edited ]

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.

Frequent Contributor
Posts: 80

Re: How to use Do statement ?

Thank you ..I will test it
Ask a Question
Discussion stats
  • 2 replies
  • 215 views
  • 2 likes
  • 2 in conversation