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!
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.
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;
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.
@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...
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.