## Recoding Variables using Arrays and Do Loops

Frequent Contributor
Posts: 144

# Recoding Variables using Arrays and Do Loops

Hi all—

I have a bunch of variables which have to be recoded into new variables. Each original variable has to be split into two variables ending in “N” (numerator) and  “D” (Denominator).  So variable Q1 is plit into Q1N and Q1D. The values the variables are assigned all depends on what the response code was for Q1 was. I have started writing out decode code for all the varibles but is there a way to do this all at once like using an Array with a Do loop or some other way.

Any help is greatly appreciated. Thanks!

data decode;

set have ;

if  Q1=1   then   Q1N=2;

else if Q1=2 then Q1N=0;

else if Q1=3 then Q1N=0;

else if Q1=4 then Q1N=1;

else if Q1=. then Q1N=0;

if  Q1=1 then    Q1D=2;

else if Q1=2 then Q1D=2;

else if Q1=3 then Q1D=0;

else if Q1=4 then Q1D=2;

else if Q1=. then Q1D=0;

if  Q2=1 then    Q2N=2;

else if Q2=2 then Q2N=0;

else if Q2=3 then Q2N=0;

else if Q2=4 then Q2N=1;

else if Q2=. then Q2N=0;

if  Q2=1 then    Q2D=2;

else if Q2=2 then Q2D=2;

else if Q2=3 then Q2D=0;

else if Q2=4 then Q2D=2;

else if Q2=. then Q2D=0;

if  Q3=1 then    Q3N=2;

else if Q3=2 then Q3N=0;

else if Q3=3 then Q3N=0;

else if Q3=4 then Q3N=1;

else if Q3=. then Q3N=0;

if  Q3=1 then    Q3D=2;

else if Q3=2 then Q3D=2;

else if Q3=3 then Q3D=0;

else if Q3=4 then Q3D=2;

else if Q3=. then Q3D=0;

Super User
Posts: 13,498

## Re: Recoding Variables using Arrays and Do Loops

For variables with exactly the same coding this should work. Make sure that if you have variables Q, QN or QD that you name your array something else and make sure the references q etc. refer to the new array reference.

The DIM statement means that SAS counts the number of elements in the array and you don't have to count them which is really helpful. If you used QN1 and QD1 naming you could use shorthand ranges like Q1 - Q25, QN1-QN25 and QD1- QD25 in the arrays. Your current naming convention requires explicitly listing the numerator and denominator variables.

array Q Q1 Q2 Q3;

array QN Q1N Q2N Q3N;

array QD Q1D Q2D Q3D;

DO I = 1 TO DIM(Q);

if Q=1 then QN=2;

else if Q=2 then QN=0;

else if Q=3 then QN=0;

else if Q=4 then QN=1;

else if Q=. then QN=0;

if Q=1 then QD=2;

else if Q=2 then QD=2;

else if Q=3 then QD=0;

else if Q=4 then QD=2;

else if Q=. then QD=0;

END;

drop I;

Super User
Posts: 6,751

## Re: Recoding Variables using Arrays and Do Loops

Also consider a slightly different grouping, such as:

if Q{i} = 1 then do;

QN{i} = 2;

QD{i} = 2;

end;

else if Q{i}=2 then do;

...

Although this will be slightly faster, that isn't the major consideration unless you are dealing with zillions of records.  In my opinion, picking something that looks easy to understand is more important.

Super Contributor
Posts: 474

## Re: Recoding Variables using Arrays and Do Loops

Hi.

Array will produce a nicer code and Macro too.

Here's one way to recreate code through Macro.

options mprint;

%macro decodevar(V,N); * V=var prefix, N=var num;

%do I=1 %to &N;

if &V&I eq 1 then do; &V&I.N=2; &V&I.D=2; end;

else if &V&I eq 2 then do; &V&I.N=0; &V&I.D=2; end;

else if &V&I eq 3 then do; &V&I.N=0; &V&I.D=0; end;

else if &V&I eq 4 then do; &V&I.N=1; &V&I.D=2; end;

else if &V&I eq . then do; &V&I.N=0; &V&I.D=0; end;

%end;

%mend decodevar;

data WANT;

set HAVE;

%decodevar(Q,4);

run;

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

Valued Guide
Posts: 765

## Re: Recoding Variables using Arrays and Do Loops

hi ... another idea (if you don't mind some LOG messages, NOTES about invalid arguments) ...

data x;

input q1 q2 q3 @@;

datalines;

1 1 1 2 2 2 3 3 3 4 4 4 . . .

;

data x;

set x;

q1n = sum(input(char('2001',q1),1.),0);

q1d = sum(input(char('2020',q1),1.),0);

q2n = sum(input(char('2001',q2),1.),0);

q2d = sum(input(char('2020',q2),1.),0);

q3n = sum(input(char('2001',q3),1.),0);

q3d = sum(input(char('2020',q3),1.),0);

run;

q1    q2    q3    q1n    q1d    q2n    q2d    q3n    q3d

1     1     1     2      2      2      2      2      2

2     2     2     0      0      0      0      0      0

3     3     3     0      2      0      2      0      2

4     4     4     1      0      1      0      1      0

.     .     .     0      0      0      0      0      0

Super User
Posts: 10,761

## Re: Recoding Variables using Arrays and Do Loops

```data decode;
q=1;n=2; d=2; output;
q=2;n=0; d=2; output;
q=3;n=0; d=0; output;
q=4;n=1; d=2; output;
q=.;n=0; d=0; output;
run;

%macro decode;
data want;
merge  %do i=1 %to &n;
decode(rename=(q=q&i n=q&i.n d=q&i.d))
%end;
;
run;
%mend;

%let n=4 ;
%decode

Ksharp

```
Discussion stats
• 5 replies
• 1053 views
• 0 likes
• 6 in conversation