BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mar0000
Obsidian | Level 7

How would I be able to say something like "if row2 BELOW row1 then flag=0"

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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,

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

What does "below" mean? Does it mean less than?

 

Are row1 and row2 names of variables, or are they actually referring to different rows?

--
Paige Miller
mar0000
Obsidian | Level 7
Row 1 and 2 are actually different rows. My variable name is 'Type' and if Type = '2' then flag=1. I want to say if Type = '5' and is below Type = '2' that has flag = 1 then flag = 0. If Type = '5' and is below Type = '2' when flag is 0 then the flag should be 1. I want to base Type '5' off of Type '2.' Whatever Type 2 has flagged, I want type 5 to do the opposite
mar0000
Obsidian | Level 7
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.
Kurt_Bremser
Super User

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.

mar0000
Obsidian | Level 7
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.

PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
mar0000
Obsidian | Level 7

Desired output 

 

Type Flag

1        1

2        1

5        0

3        1

4        1

5        1

ed_sas_member
Meteorite | Level 14

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,

Kurt_Bremser
Super User

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;

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
  • 10 replies
  • 912 views
  • 1 like
  • 4 in conversation