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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.