DATA Step, Macro, Functions and more

Cumulative Response

Accepted Solution Solved
Reply
Trusted Advisor
Posts: 1,231
Accepted Solution

Cumulative Response

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


Accepted Solutions
Solution
‎02-19-2013 08:53 AM
Super Contributor
Posts: 1,636

Re: Cumulative Response

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


All Replies
Super Contributor
Posts: 1,636

Re: Cumulative Response

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

Trusted Advisor
Posts: 1,231

Re: Cumulative Response

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


Solution
‎02-19-2013 08:53 AM
Super Contributor
Posts: 1,636

Re: Cumulative Response

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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