Hi,
I have free response data that looks like this
I want to calculate the frequency and percent of each of the individual services but I'm not sure how I can separate this out easily?
If I use the if find statement, I can create separate columns, but wouldn't be able to calculate the percent.
Can anyone help me with this?
I want
referral service | N | % |
Alternative | 10 | 5 |
CBT | 14 | 7% |
You need to separate distinct values into a new row first, then use proc freq to get frequency of categories.
eg:
data have1;
set have;
num= countc(Referral_Sevice, ',');
if num=0 then do;
Services= strip(Referral_Sevice);
output;
end;
else do i=1 to num+1;
Services= scan(Referral_Sevice, i, ',');
output;
end;
proc print;run;
proc freq data=have1;
tables services/nocol norow nocum out=want(rename=(count=n));
run;
You need to separate distinct values into a new row first, then use proc freq to get frequency of categories.
eg:
data have1;
set have;
num= countc(Referral_Sevice, ',');
if num=0 then do;
Services= strip(Referral_Sevice);
output;
end;
else do i=1 to num+1;
Services= scan(Referral_Sevice, i, ',');
output;
end;
proc print;run;
proc freq data=have1;
tables services/nocol norow nocum out=want(rename=(count=n));
run;
Is there a way to create a macro/loop to do this for multiple responses for multiple variables? Trying to do this right now but for multiple variables, any help would be super appreciated!
Yes, the program can be used inside a macro using loop to reference multiple variables sequentially.
See the below example which creates a macro parameter 'freqvars' referencing to multiple variables, then creates a macro variable 'freqvar' referencing to an individual variable (out of multiple variables) and processes each variable in a loop in the order they are referenced in macro parameter (freqvars=). This macro creates datasets for each variable with frequency of categories.
%macro freqs(freqvars=);
%do i= 1 %to %sysfunc(countw(&freqvars));
%let freqvar= %scan(&freqvars, &i);
data have1;
set have;
num= countc(&freqvar, ',');
if num=0 then do;
Services= strip(&freqvar);
output;
end;
else do i=1 to num+1;
Services= scan(&freqvar, i, ',');
output;
end;
run;
proc freq data=have1;
tables services/nocol norow nocum out=&freqvar(rename=(count=n));
run;
%end;
%mend;
%freqs(freqvars= Referral_Sevice VAR2 VAR3 ... VARn);
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.