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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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;

View solution in original post

3 REPLIES 3
Linlin
Lapis Lazuli | Level 10

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

stat_sas
Ammonite | Level 13

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


Linlin
Lapis Lazuli | Level 10

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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
  • 3 replies
  • 1705 views
  • 0 likes
  • 2 in conversation