user2568648
user2568648

Reputation: 3211

sas create variable with do loop

I have the following code :

data DEFBIS.Taux_fec_2006_2010;
set DEFBIS.Taux_fec_2006_2010;
  Taux_15 = 1000 * N_15 / (5 * P_15);
  Taux_16 = 1000 * N_16 / (5 * P_16);
  ...
  Taux_49 = 1000 * N_49 / (5 * P_49);
run;

How can I condense this code using a do statement from i = 15 to 49?

Upvotes: 0

Views: 6875

Answers (2)

Reeza
Reeza

Reputation: 21294

I would not use macro's. Use an array but set the index's to be from 15 to 49 explicitly.

data DEFBIS.Taux_fec_2006_2010;
set DEFBIS.Taux_fec_2006_2010;

array taux(15:49) taux_15-taux_49;
array nn(15:49) n_15-n_49;
array pp(15:49) p_15-p_49;

do i=15 to 49;
  Taux(i) = 1000 * nn(i)/ (5 * pp(i));
end;

run;

Upvotes: 1

Chris
Chris

Reputation: 36

Two paths you can go by....

1.Macro

%macro do_loop;

data DEFBIS.Taux_fec_2006_2010;
set DEFBIS.Taux_fec_2006_2010;

%do i = 15 %to 49;
  Taux_&i. = 1000 * N_&i. / (5 * P_&i.);
%end;

run;

%mend do_loop;

%do_loop;

2.arrays

data DEFBIS.Taux_fec_2006_2010;
set DEFBIS.Taux_fec_2006_2010;

array taux (*) taux_1 -- taux_49;
array N (*) N_1 -- N_49;
array P (*) P_1 -- P_49;

do I = 15 to 49;
  Taux (I)= 1000 * N (I)/ (5 * P (I));
end;

run;

Upvotes: 2

Related Questions