BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
393310
Obsidian | Level 7

Hi, 

I have free response data that looks like this 

393310_0-1682568618680.png

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%
1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Lapis Lazuli | Level 10

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; 

Capture.PNG

View solution in original post

4 REPLIES 4
A_Kh
Lapis Lazuli | Level 10

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; 

Capture.PNG

393310
Obsidian | Level 7
THANK YOU!!
khaque2
Calcite | Level 5

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!

A_Kh
Lapis Lazuli | Level 10

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); 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 594 views
  • 2 likes
  • 3 in conversation