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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
stat_sas
Ammonite | Level 13

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;

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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