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

Hello all,

 

I've got a dataset (a) with the following characters:

211221222223215
133122111
822823
811811811823883884
215

 

I now want to create a space between after every third position. So the new dataset (b) would be like:

211 221 222 223 215
133 122 111
822 823
811 811 811 823 883 884
215

(the actual list is about 5000 observations)

 

I have tried the prxchange function and something with an array, but its not working.

 

Hopefully you have some ideas wich i can try?

Thanks in advance

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use a do loop and substr():

data have;
input string :$30.;
cards;
211221222223215
133122111
822823
811811811823883884
215
;
run;

data want (rename=(newstring=string));
set have;
i = 1;
length newstring $30;
do until (i > length(string));
  newstring = catx(' ',newstring,substr(string,i,3));
  i + 3;
end;
drop i string;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Use a do loop and substr():

data have;
input string :$30.;
cards;
211221222223215
133122111
822823
811811811823883884
215
;
run;

data want (rename=(newstring=string));
set have;
i = 1;
length newstring $30;
do until (i > length(string));
  newstring = catx(' ',newstring,substr(string,i,3));
  i + 3;
end;
drop i string;
run;
Marcellllo
Calcite | Level 5

It works

 

Thanks a lot!

Ksharp
Super User
data have;
input string :$30.;
want=prxchange('s/(\d\d\d)/$1 /',-1,string);
cards;
211221222223215
133122111
822823
811811811823883884
215
;
run;
proc print;run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2263 views
  • 0 likes
  • 3 in conversation