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

I would like to create a flag if there are instances for "acute" locations more than once

 

groupindexlocation
11Home
12Acute
13Rehab
14Home
15Acute
21Acute
22Home
23Rehab
31Home
32Rehab
33Home
41Home
42Acute
43Home
44Acute
45Rehab

 

Want:

groupindexlocationrehospitalization
11Home0
12Acute0
13Rehab0
14Home0
15Acute1
21Acute0
22Home0
23Rehab0
31Home0
32Rehab0
33Home0
41Home0
42Acute0
43Home0
44Acute1
45Rehab0

 

or

 

grouprehospitalization
11
20
30
41

 

What I have done so far  is this:

 

proc sql;

create table test2 as

select distinct group, location, (*) as total_revisits

from test;

quit;

 

data test2; set test2;

if total_revisits >= 2 then rehopsitalization = 1

;run;

i was wondering if there was a simple way

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
  input group	index	location $;
datalines;
1	1	Home
1	2	Acute
1	3	Rehab
1	4	Home
1	5	Acute
1	6	Rehab
1	7	Home
1	8	Acute
2	1	Acute
2	2	Home
2	3	Rehab
3	1	Home
3	2	Rehab
3	3	Home
4	1	Home
4	2	Acute
4	3	Home
4	4	Acute
4	5	Rehab
;
proc sql;
create table want as
select group,sum(location='Acute')>1 as rehospitalization
 from have
  group by group;
quit;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Use a RETAINed variable in a data step:

data have;
input group index location $;
datalines;
1 1 Home
1 2 Acute
1 3 Rehab
1 4 Home
1 5 Acute
2 1 Acute
2 2 Home
2 3 Rehab
3 1 Home
3 2 Rehab
3 3 Home
4 1 Home
4 2 Acute
4 3 Home
4 4 Acute
4 5 Rehab
;

data want;
set have;
by group;
retain has_acute;
if first.group then has_acute = 0;
rehospitalization = 0;
if location = "Acute"
then do;
  if has_acute
  then rehospitalization = 1;
  has_acute = 1;
end;
drop has_acute;
run;
ballardw
Super User

A minor variation on @Kurt_Bremser's solution.

Example data includes an example with a third "Acute" to see if the result is as needed in that case.

data have;
  input group	index	location $;
datalines;
1	1	Home
1	2	Acute
1	3	Rehab
1	4	Home
1	5	Acute
1	6	Rehab
1	7	Home
1	8	Acute
2	1	Acute
2	2	Home
2	3	Rehab
3	1	Home
3	2	Rehab
3	3	Home
4	1	Home
4	2	Acute
4	3	Home
4	4	Acute
4	5	Rehab
;

data want;
   set have;
   by group;
   retain acutecount;
   if first.group then acutecount=0;
   if location='Acute' then acutecount+1;
   Flag= (location ='Acute' and acutecount>1);
   drop acutecount;
run;

SAS returns a numeric 1 for a true comparison and 0 for false. So the Flag= statement is just another way to do a sort of if <condition> then value=1; else value=0;

Ksharp
Super User
data have;
  input group	index	location $;
datalines;
1	1	Home
1	2	Acute
1	3	Rehab
1	4	Home
1	5	Acute
1	6	Rehab
1	7	Home
1	8	Acute
2	1	Acute
2	2	Home
2	3	Rehab
3	1	Home
3	2	Rehab
3	3	Home
4	1	Home
4	2	Acute
4	3	Home
4	4	Acute
4	5	Rehab
;
proc sql;
create table want as
select group,sum(location='Acute')>1 as rehospitalization
 from have
  group by group;
quit;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1424 views
  • 0 likes
  • 4 in conversation