BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cme2146
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

 

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

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.

FreelanceReinh
Jade | Level 19

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

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 1925 views
  • 0 likes
  • 4 in conversation