I have a dataset with 70 variables and thousands of subjects. About half of the subjects have multiple observations (anywhere from 2 to 13). I've created an "id" variable to figure out how many subjects I have and how many observations per subject there are. How do I find the correct frequency of participants by variable if proc freq only counts the frequency of observations?
Hello @cme2146,
PROC SQL with its count(distinct ...) syntax is a useful complement to PROC FREQ in the situation you've described. See two examples below.
/* Create test data for demonstration */
data have;
input id var1 var2 $;
cards;
1 90 B
1 105 B
1 108 A
2 85 B
3 95 C
3 101 C
3 100 A
3 97 A
4 110 C
4 104 B
4 104 A
5 75 C
5 80 C
5 80 B
5 70 B
;
proc sql;
select count(distinct id) label='Number of participants with VAR1>=100'
from have
where var1>=100;
quit;
Output:
Number of
participants
with
VAR1>=100
------------
3
proc sql;
select var2, count(distinct id) label='Number of participants'
from have
group by var2;
quit;
Output:
Number of var2 participants ---------------------- A 3 B 4 C 3
Please explain further what you want. This part isn't clear to me: " How do I find the correct frequency of participants by variable if proc freq only counts the frequency of observations?"
Also, please show a brief example.
Hint: provide an example of values similar to what you have. The sample should be small enough you can calculate the "frequency" you want by hand. Then show what the frequency should look like for the example data.
Make sure to include different types of cases.
But Participants by variable means you will have to show examples of that as well. And if any one subject has multiple different values for a given variable your example should include that as well with the desired result.
Hello @cme2146,
PROC SQL with its count(distinct ...) syntax is a useful complement to PROC FREQ in the situation you've described. See two examples below.
/* Create test data for demonstration */
data have;
input id var1 var2 $;
cards;
1 90 B
1 105 B
1 108 A
2 85 B
3 95 C
3 101 C
3 100 A
3 97 A
4 110 C
4 104 B
4 104 A
5 75 C
5 80 C
5 80 B
5 70 B
;
proc sql;
select count(distinct id) label='Number of participants with VAR1>=100'
from have
where var1>=100;
quit;
Output:
Number of
participants
with
VAR1>=100
------------
3
proc sql;
select var2, count(distinct id) label='Number of participants'
from have
group by var2;
quit;
Output:
Number of var2 participants ---------------------- A 3 B 4 C 3
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.