Hi All,
I've a data set containing 3000 respondents with 7 variables. Each of the 7 variables containing 1 to 7 values/responses. I need to know how many times each of 7 values appearing for each respondent.
Thanks in advance for suggesting a solution!
Naeem
Hi,
I added the RED line to fix the missing problem.
data have;
input id v1-v7;
cards;
7 7 7 7 7 7 7 7
1 1 2 3 4 5 6 7
2 3 4 5 4 3 3 2
3 4 5 6 7 6 5 4
4 3 4 5 6 4 3 2
5 5 5 5 5 5 5 5
6 2 2 2 2 2 2 2
;
data want(drop=i j);
set have;
retain answer1-answer7 0;
array _v(*) v1-v7;
array _w(*) answer1-answer7 ;
do i=1 to 7;do j=1 to 7;
if _v(j)=i then _w(i)+1;
end;end;output;
do i=1 to 7;
_w(i)=0;
end;
proc print;run;
/* you could use informat to convert the responses to numbers. example */
data test;
input a $20.;
cards;
Less than 1 hour
2-3 hours
4-5 hours
6-7 hours
8-9 hours
10-15 hours
16 hours or more
;
proc format;
invalue trial
'Less than 1 hour'=1
'2-3 hours'=2
'4-5 hours'=3
'6-7 hours'=4
'8-9 hours'=5
'10-15 hours'=6
'16 hours or more'=7;
data want;
set test;
b=input(a,trial.);
proc print;run;
data have;
input id v1-v7;
cards;
1 1 2 3 4 5 6 7
2 3 4 5 4 3 3 2
3 4 5 6 7 6 5 4
4 3 4 5 6 4 3 2
5 5 5 5 5 5 5 5
6 2 2 2 2 2 2 2
;
data want(drop=i j);
set have;
array _v(*) v1-v7;
array _w(*) answer1-answer7;
do i=1 to 7;do j=1 to 7;
if _v(j)=i then _w(i)+1;
end;end;output;
do i=1 to 7;
_w(i)=0;
end;
proc print;run;
Obs id v1 v2 v3 v4 v5 v6 v7 answer1 answer2 answer3 answer4 answer5 answer6 answer7
1 1 1 2 3 4 5 6 7 1 1 1 1 1 1 1
2 2 3 4 5 4 3 3 2 0 1 3 2 1 0 0
3 3 4 5 6 7 6 5 4 0 0 0 2 2 2 1
4 4 3 4 5 6 4 3 2 0 1 2 2 1 1 0
5 5 5 5 5 5 5 5 5 0 0 0 0 7 0 0
6 6 2 2 2 2 2 2 2 0 7 0 0 0 0 0
Hi Linlin;
Thanks for suggesting a solution for this problem. If we have following 7 labels in 7 questions instead of (1-7) numbers then how can we modify syntax please
Less than 1 hour
2-3 hours
4-5 hours
6-7 hours
8-9 hours
10-15 hours
16 hours or more
Also I am getting missing values in the output in first row because my first row contains 7 for all of 7 questions.
Naeem
Hi,
I added the RED line to fix the missing problem.
data have;
input id v1-v7;
cards;
7 7 7 7 7 7 7 7
1 1 2 3 4 5 6 7
2 3 4 5 4 3 3 2
3 4 5 6 7 6 5 4
4 3 4 5 6 4 3 2
5 5 5 5 5 5 5 5
6 2 2 2 2 2 2 2
;
data want(drop=i j);
set have;
retain answer1-answer7 0;
array _v(*) v1-v7;
array _w(*) answer1-answer7 ;
do i=1 to 7;do j=1 to 7;
if _v(j)=i then _w(i)+1;
end;end;output;
do i=1 to 7;
_w(i)=0;
end;
proc print;run;
/* you could use informat to convert the responses to numbers. example */
data test;
input a $20.;
cards;
Less than 1 hour
2-3 hours
4-5 hours
6-7 hours
8-9 hours
10-15 hours
16 hours or more
;
proc format;
invalue trial
'Less than 1 hour'=1
'2-3 hours'=2
'4-5 hours'=3
'6-7 hours'=4
'8-9 hours'=5
'10-15 hours'=6
'16 hours or more'=7;
data want;
set test;
b=input(a,trial.);
proc print;run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.