I have a dataset with 4 variables that basically is the same variable that house intervention codes. Is there a proc freq code that I can use to treat the 4 variables the same variable to get an overall frequency distribution of all 4 variables? Here is the sample dataset.
data SAMPINT;
infile datalines dsd truncover;
input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;
Essentially, I would like the output to look like this:
Intervention | Frequency |
BB8 | 7 |
ABC | 5 |
CCC | 1 |
Thanks alot for your help!
Something like this?
data SAMPINT;
infile datalines dsd truncover;
input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;
proc transpose data=SAMPINT
out=long_SAMPINT(rename=(Col1=Intervention) drop = _NAME_);
by ID;
var Intervention1-Intervention4;
run;
proc freq data = long_SAMPINT;
tables Intervention / nocum nopercent;
run;
Something like this?
data SAMPINT;
infile datalines dsd truncover;
input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;
proc transpose data=SAMPINT
out=long_SAMPINT(rename=(Col1=Intervention) drop = _NAME_);
by ID;
var Intervention1-Intervention4;
run;
proc freq data = long_SAMPINT;
tables Intervention / nocum nopercent;
run;
Thanks. Is there a way to limit the final output to the top 5 observations in descending order?
If you are fond of writing code, using a hash object can replace both procs.
data _null_;
set work.sampint end=done;
length Intervention $ 3 Frequency 8;
array inter Intervention1-Intervention4;
if _n_ = 1 then do;
declare hash h(/*ordered: 'YES'*/);
h.defineKey('Intervention');
h.defineData('Intervention', 'Frequency');
h.defineDone();
end;
do i = 1 to dim(inter);
if not missing(inter[i]) then do;
Intervention = inter[i];
if h.find() ^= 0 then do;
Frequency = 1;
h.add();
end;
else do;
Frequency = Frequency + 1;
h.replace();
end;
end;
end;
if done then do;
h.output(dataset: 'work.want');
end;
run;
proc print data=work.want;run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: