Macro to loop through variables and store results SAS -


i have following variables: a_bldg b_bldg c_bldg d_bldg. want multiply them intsf , store result in new variable, sale_i. example, a_bldg * intsf = sale_a, b_bldg * intsf = sale_b, , on.

my code is:

%macro loopit(mylist); %let n=%sysfunc(countw(&mylist)); %do j = 1 %to &n; %let = %scan(&mylist,&j);    data test;   set data;   sale_&i. = &i._bldg * intsf;   run;  %end; %mend;  %let list = b c d; %loopit(&list); 

this produces sale_d, last letter in list. how sales a-c appear? first 4 lines of code can loop through text a-d. thought doing arrays, didn't know how choose variables based on a-d indicators. help!

you're looping through list , recreating test dataset every time, appears have sale_d because you're viewing last iteration.

you can clean loop scanning through list in 1 data step solve problem:

%let list = b c d;  %macro loopit;      data test;         set data;             %do = 1 %to %sysfunc(countw(&list.));             %let this_letter = %scan(&list., &i.);                 sale_&this_letter. = &this_letter._bldg * intsf;             %end;     run;  %mend loopit;  %loopit; 

Comments