How would I be able to say something like "if row2 BELOW row1 then flag=0"
Hi @mar0000
Does the following code meet your expectations?
The LAG() function retrieves the value from the previous row.
data want;
set have;
flag = 1;
_lag = lag(Type);
if (Type=2 and _lag=5) or (Type=5 and _lag=2) then flag=0;
drop _lag;
run;
Best,
What does "below" mean? Does it mean less than?
Are row1 and row2 names of variables, or are they actually referring to different rows?
PLEASE
supply example data in usable form (data step(s) with datalines), so we all don't waste time guessing what you have and what you want.
Data have;
Type;
1
2
5
3
4
5
;
data want;
set have;
if type = '2' then flag = 1;
I want to say: if type = 2 and flag = 1 then when type = 5, the flag should be 0. If type = 5 is by itself (such as the last line of data), then the flag should be 1.
In case I forgot to mention: A WORKING data step that creates a dataset, and not just a log full of ERRORs.
@mar0000 wrote:
This may make more sense: if Type 2 is flagged 1, I want Type 5 to flag 0. If Type 2 is flagged 0, I want Type 5 to flag 1.
So what does this have to do with rows? Can you please show us an example data set, and then show us the desired output?
Desired output
Type Flag
1 1
2 1
5 0
3 1
4 1
5 1
Hi @mar0000
Does the following code meet your expectations?
The LAG() function retrieves the value from the previous row.
data want;
set have;
flag = 1;
_lag = lag(Type);
if (Type=2 and _lag=5) or (Type=5 and _lag=2) then flag=0;
drop _lag;
run;
Best,
So you seem to mean:
when a type of 5 immediately follows a type of 2, flag should be 0, otherwise it should be zero?
If that is the case:
data want;
set have;
flag = not (type = 5 and lag(type) = 2);
run;
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!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.