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

Hi SAS community,

 

I have a longitudinal cohort dataset. I need to keep only participants with urban = 1 at baseline. 

 

Dataset1

UniqIDyearurban
10120011
10120021
10120031
10120041
10220010
10220020
10220031
10220041
10320010
10320020
10320030
10320041
10420011
10420020
10420030
10420040

 

Needs to look like this:

 

UniqIDyearurban
10120011
10120021
10120031
10120041
10420011
10420020
10420030
10420040

 

Can someone please help me!

 

Justin

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Justin_Lang wrote:

Hi SAS community,

 

I have a longitudinal cohort dataset. I need to keep only participants with urban = 1 at baseline. 

 

Dataset1

UniqID year urban
101 2001 1
101 2002 1
101 2003 1
101 2004 1
102 2001 0
102 2002 0
102 2003 1
102 2004 1
103 2001 0
103 2002 0
103 2003 0
103 2004 1
104 2001 1
104 2002 0
104 2003 0
104 2004 0

 

Needs to look like this:

 

UniqID year urban
101 2001 1
101 2002 1
101 2003 1
101 2004 1
104 2001 1
104 2002 0
104 2003 0
104 2004 0

 

Can someone please help me!

 

Justin


See if this gets you started.

/* assumes input set sorted by ID and year*/
data want;
  set have;
  by uniqid ;
  retain flag;
  if first.uniqid then flag= (urban=1);
  if flag;
  drop flag;
run;

From the way you present your example data I suspect that your Urban value might actually be character. If the above code gives you messages about conversion to character messages then likely you should change the urban=1 to urban='1'.

The key part is the BY processing sets an automatic variable referenced as first.uniqid that is true for the first record with the value. The Flag = does a logical comparison of the Urban value an if true will have a value of 1(true) or 0 (false). The Retain statement keeps the value of flag for the next iteration of the data step. The If Flag subsets the data to have only the values where Flag is true.

Since you really wouldn't need the value of flag later I drop it.

You could test the logic of the code without the IF flag; and Drop Flag ; statements to see the result of the logic.

View solution in original post

2 REPLIES 2
ballardw
Super User

@Justin_Lang wrote:

Hi SAS community,

 

I have a longitudinal cohort dataset. I need to keep only participants with urban = 1 at baseline. 

 

Dataset1

UniqID year urban
101 2001 1
101 2002 1
101 2003 1
101 2004 1
102 2001 0
102 2002 0
102 2003 1
102 2004 1
103 2001 0
103 2002 0
103 2003 0
103 2004 1
104 2001 1
104 2002 0
104 2003 0
104 2004 0

 

Needs to look like this:

 

UniqID year urban
101 2001 1
101 2002 1
101 2003 1
101 2004 1
104 2001 1
104 2002 0
104 2003 0
104 2004 0

 

Can someone please help me!

 

Justin


See if this gets you started.

/* assumes input set sorted by ID and year*/
data want;
  set have;
  by uniqid ;
  retain flag;
  if first.uniqid then flag= (urban=1);
  if flag;
  drop flag;
run;

From the way you present your example data I suspect that your Urban value might actually be character. If the above code gives you messages about conversion to character messages then likely you should change the urban=1 to urban='1'.

The key part is the BY processing sets an automatic variable referenced as first.uniqid that is true for the first record with the value. The Flag = does a logical comparison of the Urban value an if true will have a value of 1(true) or 0 (false). The Retain statement keeps the value of flag for the next iteration of the data step. The If Flag subsets the data to have only the values where Flag is true.

Since you really wouldn't need the value of flag later I drop it.

You could test the logic of the code without the IF flag; and Drop Flag ; statements to see the result of the logic.

Justin_Lang
Fluorite | Level 6

Thanks for the reply. This seems to have worked for me!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 497 views
  • 0 likes
  • 2 in conversation