Use an array.
See an example here:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001371428.htm
Something like this?
data have;
input Acct $ (col1-col3) ($);
cards;
ABC 45 375 89756
TFA 609 8320 835
FGT 5907 561 413
;
run;
data want;
set have;
array cols {*} col1-col3;
array newphone {*} $ newphone1-newphone3;
do i = 1 to 3;
newphone{i} = cats(put(length(cols{i}),z2.),cols{i});
end;
drop i;
run;
Some questions. Firstly, why are you trying to do this using text numbers? Numeric data types are there for a reason. Secondly, what is the reasoning behind this, newphone would end up looking like:
0245
03609
045907
Which doesn't really make sense to me. What is col1-col3, it looks from the var name newphone, to be a number, but catting on an 02/03 etc. wouldn't make the result a number. I mean you "could" do:
data have; input Acct $ col1 $ col2 $ col3 $; datalines; ABC 45 375 89756 TFA 609 8320 835 FGT 5907 561 413 ; run; data want; length newphone $20; set have; newphone=cats("0",put(lengthn(col1),best.),col1); run;
But why?
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.