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 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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