Problems with a loop

Reply
N/A
Posts: 0

Problems with a loop

Here is the program:

%macro vacnumeric (nprefix);
%do i = 1 %to &nprefix;
%let prefix1=%str(fur);
%let prefix2=%str(finearts);
%let prefix3=%str(jewel);
%let prefix4=%str(silver);
data newv66;
set newv55;
length &&prefix&i._pol_vac_tpremn 8.;
length &&prefix&i._blkt_amtn 8.;
length &&prefix&i._item_amtn 8.;
length &&prefix&i._nbr_item 8.;
length &&prefix&i._sub_class_tot_tpremn 8.;
&&prefix&i._pol_vac_tpremn = input(compress(&&prefix&i._pol_vac_tprem),8.);
&&prefix&i._blkt_amtn = input(compress(&&prefix&i._blkt_amt),8.);
&&prefix&i._item_amtn = input(compress(&&prefix&i._item_amt),8.);
&&prefix&i._nbr_itemn = input(compress(&&prefix&i._nbr_item),8.);
&&prefix&i._sub_class_tot_tpremn = input(compress(&&prefix&i._sub_class_tot_tpremn),8.);
run;
%end;
%mend vacnumeric;

%vacnumeric(4);

This is running incorrectly. For example for the first variable in the 1st run of the loop &&prefix&i._pol_vac_tpremn resolves to prefix1_pol_vac_tpremn. I wanted it to resolve to fur_pol_vac_tpremn. Additionally since this referencing isn't being resolved, I can't check if these twenty variable character to numeric changes will be in work.newv66 (or will the second 5 overwrite the first 5, third 5 overwrite the second 5, etc.) Can someone help me remedy this or offer a more efficient solution? Thanks.
Frequent Contributor
Posts: 81

Re: Problems with a loop

The problem lies in the 2nd resolution of the macro variables:
A single point after the macro variables is lost after it is resolved:
&&prefix&i._xxx
&prefix4_xxx
???

so you'll have to add another point:
&&prefix&i.._xxx
&prefix4._xxx
silver_xxx

The corrected code is:
%macro vacnumeric (nprefix);
%do i = 1 %to &nprefix;
%let prefix1=%str(fur);
%let prefix2=%str(finearts);
%let prefix3=%str(jewel);
%let prefix4=%str(silver);
data newv66;
set newv55;
length &&prefix&i.._pol_vac_tpremn 8;
length &&prefix&i.._blkt_amtn 8;
length &&prefix&i.._item_amtn 8;
length &&prefix&i.._nbr_item 8;
length &&prefix&i.._sub_class_tot_tpremn 8;
&&prefix&i.._pol_vac_tpremn = input(compress(&&prefix&i.._pol_vac_tprem),8.);
&&prefix&i.._blkt_amtn = input(compress(&&prefix&i.._blkt_amt),8.);
&&prefix&i.._item_amtn = input(compress(&&prefix&i.._item_amt),8.);
&&prefix&i.._nbr_itemn = input(compress(&&prefix&i.._nbr_item),8.);
&&prefix&i.._sub_class_tot_tpremn = input(compress(&&prefix&i.._sub_class_tot_tpremn),8.);
run;
%end;
%mend vacnumeric;

%vacnumeric(4);

........Phil
N/A
Posts: 0

Re: Problems with a loop

I rewrote the program with the extra . as you mentioned. However, I moved the DO statement inside the data step to prevent work.newv66 from being re-written after each iteration. However, this causes an error that indicates that the LENGTH statement is used out of proper order. Any idea why this is happening?

%macro vacnumeric (nprefix);
%let prefix1=%str(fur);
%let prefix2=%str(finearts);
%let prefix3=%str(jewel);
%let prefix4=%str(silver);
data newv66;
set newv55;
%do i = 1 %to &nprefix;
length &&prefix&i.._pol_vac_tpremn 8.;
length &&prefix&i.._blkt_amtn 8.;
length &&prefix&i.._item_amtn 8.;
length &&prefix&i.._nbr_itemn 8.;
length &&prefix&i.._sub_class_tot_tpremn 8.;
&&prefix&i.._pol_vac_tpremn = input(compress(&&prefix&i.._pol_vac_tprem),8.);
&&prefix&i.._blkt_amtn = input(compress(&&prefix&i.._blkt_amt),8.);
&&prefix&i.._item_amtn = input(compress(&&prefix&i.._item_amt),8.);
&&prefix&i.._nbr_itemn = input(compress(&&prefix&i.._nbr_item),8.);
&&prefix&i.._sub_class_tot_tpremn = input(compress(&&prefix&i.._sub_class_tot_tpremn),8.);
run;

%end;
%mend vacnumeric;

%vacnumeric(4);
New Contributor
Posts: 3

Re: Problems with a loop

Hi,

You can't change the length of a variable once it has been created on the PDV. It sounds like at least one of the variables referenced in one of your LENGTH statements already exists in NEWV55. Try moving your LENGTH statements before the SET statement in your DATA step.

Hope this helps,
Nancy
Post a Question
Discussion Stats
  • 3 replies
  • 366 views
  • 0 likes
  • 3 in conversation