BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
3 REPLIES 3
prholland
Fluorite | Level 6
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
deleted_user
Not applicable
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);
Nancy_B
Calcite | Level 5
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 3 replies
  • 1102 views
  • 0 likes
  • 3 in conversation