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

Hi.

 

I am using SAS 9.4 and have a programming question

 

I am looking at encounters (each encounter has multiple rows but not of the same number of rows throughout the dataset eg one encounter may have 10 rows and another have only 2) and I want to see if a code 1 and a code 2 is present with the multiple encounters. I have set up the dataset with a retain statement such that each encounter is label 1 ,2 ,3 etc. Is it possible to achieve this in SAS? Thank you for the help! 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @GS2,

 

Try this:

 

/* Create test data for demonstration */

data have;
input enc code;
cards;
1 3
1 2
1 2
1 4
2 2
2 4
2 3
2 1
2 5
3 22
3 1
3 1
4 1
4 0
4 2
;

/* Select encounters with both code 1 and code 2 */

data want(drop=_:);
do until(last.enc);
  set have;
  by enc;
  if code=1 then _c1=1;
  else if code=2 then _c2=1;
end;
do until(last.enc);
  set have;
  by enc;
  if _c1 & _c2 then output;
end;
run;

If you don't need all observations from the selected encounters, but just their numbers, replace the entire second DO loop by a subsetting IF statement:

 

if _c1 & _c2;

and change drop=_: to keep=enc.

 

 

View solution in original post

4 REPLIES 4
rbikes
Obsidian | Level 7

Could you do something like this? instead of sashelp.class use your dataset and instead of AGE use the code var, and use 1 and 2 for 14 and 15.

 

proc freq data=sashelp.class;
	where AGE in (14 15);
	table age;
run;
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

I thought about that at first. However, using this method it returns every time there is a 1 or 2 and I want it to only return if their is a 1 and 2. 

PaigeMiller
Diamond | Level 26

@GS2 wrote:

Hi.

 

I am using SAS 9.4 and have a programming question

 

I am looking at encounters (each encounter has multiple rows but not of the same number of rows throughout the dataset eg one encounter may have 10 rows and another have only 2) and I want to see if a code 1 and a code 2 is present with the multiple encounters. I have set up the dataset with a retain statement such that each encounter is label 1 ,2 ,3 etc. Is it possible to achieve this in SAS? Thank you for the help! 


Sure would be nice if you can show us a small example. Please use these instructions to provide your small example as a SAS data step: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hi @GS2,

 

Try this:

 

/* Create test data for demonstration */

data have;
input enc code;
cards;
1 3
1 2
1 2
1 4
2 2
2 4
2 3
2 1
2 5
3 22
3 1
3 1
4 1
4 0
4 2
;

/* Select encounters with both code 1 and code 2 */

data want(drop=_:);
do until(last.enc);
  set have;
  by enc;
  if code=1 then _c1=1;
  else if code=2 then _c2=1;
end;
do until(last.enc);
  set have;
  by enc;
  if _c1 & _c2 then output;
end;
run;

If you don't need all observations from the selected encounters, but just their numbers, replace the entire second DO loop by a subsetting IF statement:

 

if _c1 & _c2;

and change drop=_: to keep=enc.

 

 

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
  • 4 replies
  • 786 views
  • 0 likes
  • 4 in conversation