I have a string of the following numbers which I would like to separate with commas.
000100002000300040005
Expected:
0001,0002,0003,0004,0005
I do not at any point know the length of the string.
Thanks in advance
i = 1;
do while (i < length(invar));
outvar = catx(',',outvar,substr(invar,i,4));
i + 4;
end;
drop i;
i = 1;
do while (i < length(invar));
outvar = catx(',',outvar,substr(invar,i,4));
i + 4;
end;
drop i;
Are they always four characters? If not how do you know where one starts and one stops?
If they are always four then:
data want;
str="000100002000300040005";
length new_str $200;
do i=0 to lengthn(str)/4;
new_str=catx(",",new_str,substr(str,(i*4)+1,4));
end;
run;
However that doesn't get your output as 00002 is not four. You need to have some logical base to split the string.
Just for fun.
data have;
x='00010002000300040005';
length want $ 200;
do i=1 to length(x);
if mod(i,4)=1 then want=catx(',',want,char(x,i));
else want=cats(want,char(x,i));
end;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.