Help using Base SAS procedures

Array Help

Accepted Solution Solved
Reply
Contributor
Posts: 69
Accepted Solution

Array Help

Hello I am having some issues with my array. I am trying to turn a 5-tiered scoring system into a three tiered scoring for thirteen questions. The issues is that it only works for the first variable (Q1) and keeps the same values for the other variables. Does anyone have an idea of what might be going on? Thank you!

DATA TEST2;

SET TEST;

array oldscore (13) Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

array newscore (13) Q1_new Q2_new Q3_new Q4_new Q13_new Q14_new Q15_new

Q16_new Q17_new Q18_new Q19_new Q20_new Q21_new;

do i = 1 to 13;

do j = 1 to 13;

if oldscore(i) GE 1 AND oldscore(i) LE 2 THEN newscore(j) = 1;

if oldscore(i)= 3                        THEN newscore(j) = 2;

if oldscore(i) GE 4 AND newscore(i) LE 5 THEN newscore(j) = 3;

end;

end;

drop i j;

RUN;


Accepted Solutions
Solution
‎09-02-2014 11:01 AM
Super User
Posts: 10,538

Re: Array Help

If you are assigning these the way I think you mean, then you do not want the J loop. When you use I=1 to 13 and J=1 to 13 you end up with 13*13 loops.

BTW for analysis you might consider using a format instead of adding variables especially if you're primarily concerned with counts.

Or use invalue to create a custom informat.

proc format;

invalue myinvalue

1,2 = 1

3=2

4,5=3;

value myvalue

1,2 = 1

3=2

4,5=3;

run;

data test2;

SET TEST;

array oldscore (13) Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

array newscore (13) Q1_new Q2_new Q3_new Q4_new Q13_new Q14_new Q15_new

Q16_new Q17_new Q18_new Q19_new Q20_new Q21_new;

do I=1 to dim(oldscore);

     newscore = input(oldscore,myinvalue.);

end;

run;

The custom informat approach, or format, has an advantage that if you want to change the coding to 1=1, 2 3 and 4 =2 and 5=3 then your code change is minimal.

Or using the format

Proc freq data=test;

tables Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

format Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21 myvalue.;

run;

without creating new variables. The format will also work for grouping data in other procedures and graphs.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,204

Re: Array Help

Try this, I think only one do loop is enough.

DATA TEST2;

SET TEST;

array oldscore (13) Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

array newscore (13) Q1_new Q2_new Q3_new Q4_new Q13_new Q14_new Q15_new

Q16_new Q17_new Q18_new Q19_new Q20_new Q21_new;

do i = 1 to 13;

if oldscore(i) GE 1 AND oldscore(i) LE 2 THEN newscore(i) = 1;

if oldscore(i)= 3                        THEN newscore(i) = 2;

if oldscore(i) GE 4 AND newscore(i) LE 5 THEN newscore(i) = 3;

end;

drop i;

RUN;

Solution
‎09-02-2014 11:01 AM
Super User
Posts: 10,538

Re: Array Help

If you are assigning these the way I think you mean, then you do not want the J loop. When you use I=1 to 13 and J=1 to 13 you end up with 13*13 loops.

BTW for analysis you might consider using a format instead of adding variables especially if you're primarily concerned with counts.

Or use invalue to create a custom informat.

proc format;

invalue myinvalue

1,2 = 1

3=2

4,5=3;

value myvalue

1,2 = 1

3=2

4,5=3;

run;

data test2;

SET TEST;

array oldscore (13) Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

array newscore (13) Q1_new Q2_new Q3_new Q4_new Q13_new Q14_new Q15_new

Q16_new Q17_new Q18_new Q19_new Q20_new Q21_new;

do I=1 to dim(oldscore);

     newscore = input(oldscore,myinvalue.);

end;

run;

The custom informat approach, or format, has an advantage that if you want to change the coding to 1=1, 2 3 and 4 =2 and 5=3 then your code change is minimal.

Or using the format

Proc freq data=test;

tables Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21;

format Q1 Q2 Q3 Q4 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21 myvalue.;

run;

without creating new variables. The format will also work for grouping data in other procedures and graphs.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 180 views
  • 3 likes
  • 3 in conversation