BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sir_Lancelot
Fluorite | Level 6

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

1 ACCEPTED SOLUTION
3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Ksharp
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1510 views
  • 1 like
  • 4 in conversation