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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 855 views
  • 0 likes
  • 2 in conversation