if statement - SAS create 10 new variables according to 10 existing one with a do loop -


i have par file, 10 variables: q1-q10 has values in range of 0-1. need create each variable (q1,q2,q3...) new variable receive values of 1-5 according following range 0< q <0.2 => 1

0.2< q < 0.4 => 2 , on..

eventually each q should new column should additional column values 1-5

i have managed create 1 variable, how can 10 of them in more efficient way?

 data par1; set par; if q1>0 , q1<0.2 do; qq1=1;end; if q1>0.2 , q1<0.4 do; qq1 =2;end; if q1>0.4 , q1<0.6 do; qq1=3;end; if q1>0.6 , q1<0.8 do; qq1=4;end; if q1>0.8 , q1<1 do; qq1=5;end; run; proc print data=par1; run; 

you can use array.

data par1;     set par;      array q{*} q1-q10;     array qq{10};      = 1 dim(q);         if 0 < q[i] < 0.2 qq[i] = 1;         else if 0.2 < q[i] < 0.4 qq[i] = 2;         else if 0.4 < q[i] < 0.6 qq[i] = 3;         else if 0.6 < q[i] < 0.8 qq[i] = 4;         else if 0.8 < q[i] < 1 qq[i] = 5;     end;      drop i; run; 

the first array statement creates array consisting of 10 q variables. second initializes 10 new qq variables, named sequentially qq1 through qq10.

this loops on variables index, i.e. position, in array. q[2] references q2 , on. each variable, new variable assigned based on logic provided.

as aside, note have inequalities in logic not equalities--this result in missing values when, example, q1 = 0.2.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -