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

Hello!

 

I'm trying to count, how many times in total did the respondents answer a question X during the longitudinal study. I would like to have a mean and standard deviation.

 

Example:

NAME           QUESTION X                   QUESTION X              QUESTION X

                        AT POINT 1                   AT POINT 2                    AT POINT 3

Matt                       3                                         2                                     1           (three times)     

Lisa                       4                                                                                4            (two times)

Anne                                                                3                                      2           (two times)

Karl                       3                                                                                              (one time)

 

I would like to know how many times in total (mean and SD) the participants provided information on the question X? In the example, I can see that the mean is 2, but I don't know how to make the script in SAS.

 

Thank you so much for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data have ;
	infile cards ;
	input 
		name $
		q1
		q2
		q3 ;
cards ;
matt 3 2 1
lisa 4 . 4
anne . 3 2
karl 3 . . 
;

data have2;
    set have;
    qcnt = n(of q1-q3);
run;

proc means data=temp1 ;
	var qCnt ;
run ;
--
Paige Miller

View solution in original post

4 REPLIES 4
AMSAS
SAS Super FREQ

Here's one way to do it using some data step then pushing the results into PROC MEANS

 

/* Create Test Data */
data have ;
	infile cards ;
	input 
		name $
		q1 $
		q2 $
		q3 $ ;
cards ;
matt 3 2 1
lisa 4 . 4
anne . 3 2
karl 3 . . 
;

/* Add variable that counts number of responses to questions */

data temp1 ;
	set have ;
	array qs{3} q1-q3 ;
	qCnt=0 ;
	do i=1 to 3 ;
		if not missing(qs{i}) then
			qCnt+1 ;
		/* output values to log for testing */
		put name= qs{i}= qCnt= numberOfObs= ;
	end ;
	/* Output observation */
	output ;	
run ;

/* PROC MEANS provides mean and standard deviation */
proc means data=temp1 ;
	var qCnt ;
run ;
PaigeMiller
Diamond | Level 26
data have ;
	infile cards ;
	input 
		name $
		q1
		q2
		q3 ;
cards ;
matt 3 2 1
lisa 4 . 4
anne . 3 2
karl 3 . . 
;

data have2;
    set have;
    qcnt = n(of q1-q3);
run;

proc means data=temp1 ;
	var qCnt ;
run ;
--
Paige Miller
Mari4
Fluorite | Level 6
Thank you for your quick help!

I used this command, and I got the result! So, I think that I managed to do this! 😄 I'm really happy to have help via this Community! 🙂

Mari4
Fluorite | Level 6
Thank you for your help!
This array command is little strange to me, so I used the script below. I have to familiarize myself also to this array command! 🙂

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1175 views
  • 3 likes
  • 3 in conversation