BookmarkSubscribeRSS Feed
slivingston1
Calcite | Level 5
Good morning,

I am trying to code for a situation I have never seen before.
I am trying to display a frequency table of different facilities and the amount of physical restraints used divided by types. IE>

(Facility ID) (Restr 1) (Restr 2)
1 42 35
2 342 32
3 23423 343

Each row is considered an admission to the facility, having a patient SSN attached to it. I also need to determine how many patients per restraint per facility. This would be determined by how many different social security numbers per restraint per facility ID , and somehow doing a count function.
The data is similar to this….

(Facility ID) ( SSN) ( Restr 1) (Restr 2)
1 1233 1 0
1 1233 1 0
1 1554 0 1
2 1111 1 0
2 1224 0 1
2 1224 0 1
Legend: restraint1=0, no restraint used, restraint 1=1, restraint used

I want a table to be generated from the above sample data to look like this…

(Facility ID) (Restr1) (# of Patients used restr1) (Restr 2) (# of Patients used restr2)
1 2 1 1 1
2 1 1 2 1

I am using enterprise as well. I suppose it has something to do with DupOUT but I am not as familiar with that function. Any help would be greatly appreciated!
3 REPLIES 3
Reeza
Super User
I don't think you can do what you need in one step.
But here's something to help you get started. It will generate two tables, one with counts and one with distinct counts.


*input sample data;
data sample;
input facility_id ssn rstr_1 rstr_2;
cards;
1 1233 1 0
1 1233 1 0
1 1554 0 1
2 1111 1 0
2 1224 0 1
2 1224 0 1
;
run;

*# of restraints;
proc means data=sample sum;
class facility_id;
var rstr_1 rstr_2;
output out=keep1 sum=;
run;

*get rid of duplicates;
proc sort data=sample ;
by facility_id ssn rstr_1 rstr_2;
run;

proc sort data=sample noduprecs dupout=duplicates;
by facility_id ssn rstr_1 rstr_2;
run;

*unique counts;
proc means data=sample sum;
class facility_id;
var rstr_1 rstr_2;
output out=keep2 sum=;
run;
SUN59338
Obsidian | Level 7
proc sort data=sample ;
by facility_id ;
run;
*if your data already sorted by facility_id, you don't need sort it again.;
* in the result, the restr1 will be the freqency restr1 in (0 1), the restr1_count will be the freqency for restr1=1;
* same for restr2;
*if there are no missing values exist, restr1 will be equqal to restr2;

data result;
set sample;
by facility_id;
drop count1 count2 ssn;
if first.facility_id then do;
count1=0;
count2=0;
restr1_count=0;
restr2_count=0;
end;
if restr1 eq 1 then restr1_count+1;
else if restr1 eq 0 then count1+1;
if restr2 eq 1 then restr2_count+1;
else if restr2 eq 0 then count2+1;
if last.facility_id then do;
restr1=sum(count1,restr1_count);
restr2=sum(count2,restr2_count);
output;
end;
run;
ballardw
Super User
If I understand that you are looking for a count of valid records for each rstr (non-missing) and the number of ones, then try this for generating a table of frequencies.

/* using one of the example data sets above*/

proc tabulate data=sample;
class facility_id;
var rstr_1 rstr_2;
table facility_id, (rstr_1 rstr_2)*(n='Patients' sum='Restrained');
run;

If this looks like what you want but need an output dataset tabulate supports output datasets as well.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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