This is a classic SAS macro issue of timing. There are two main things you need to change in your code.
Use the PUTN Function instead of the PUT Function. The PUTN Function enables you to specify a numeric format at run time. The Put Function does so at compile time, i.e. before the loop executes.
Use the Symget Function in the Data Step to retrieve the value of the macro variable during DATA step execution (not when it compiles).
I changed the input values a bit, to make the result clearer. Feel free to ask.
data df1;
a = .; b=1.235; c=1.234; output;
a = 1.234; b=.; c=1.234; output;
a = 1.234; b=1.234; c=.; output;
run;
%let _width1=5.1;
%let _width2=5.2;
%let _width3=5.3;
data want ;
set df1;
array _invars{3} a b c;
array _inval{3} $16 ;
do i = 1 to 3;
f = symget(cats('_width', i));
_inval{i}=putn(_invars{i}, f);
end;
run;
Result:
a b c _inval1 _inval2 _inval3 i f
. 1.235 1.234 . 1.24 1.234 4 5.3
1.234 . 1.234 1.2 . 1.234 4 5.3
1.234 1.234 . 1.2 1.23 . 4 5.3
... View more