- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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% |
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);