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;
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.
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;
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.
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.