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

Greetings community,

 

I have this type of dataset:

 

id

A123B12C123

 

Here is what I need:

 

id

A123-B12-C123

 

Can you please let me know how to convert this dataset?

 

Thank you,

Altijani

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

newId = catx("-", substr(id,1,4), substr(id,5,3), substr(id,8));

PG

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

It's really hard to generalize from a single example.

 

Do you want dashes between numbers that are followed by a letter? Or dashes after the fourth character and after the 7th character? Or is there some other rule?

--
Paige Miller
altijani
Quartz | Level 8

The rule has multiple combinations. What I want is to break down the field by 4 (characters/numbers) followed by a dash, then 3 (characters/numbers) followed by a dash, and then will be 4 (characters/Numbers) left at the end.

PaigeMiller
Diamond | Level 26
data want;
    set have;
    length newstring $ 13;
    str1=substr(text,1,4);
    str2=substr(text,5,3);
    str3=substr(text,8,4);
    newstring = cats(str1,'-',str2,'-',str3);
run;
--
Paige Miller
PGStats
Opal | Level 21

newId = catx("-", substr(id,1,4), substr(id,5,3), substr(id,8));

PG
andreas_lds
Jade | Level 19

Fully agree to @PaigeMiller : please post more examples or describe the rule to be implemented.

 

I guessed that a dash should be inserted between digit and letter. If "id" is 11 chars long, you can't store the version with dashes in it.

 

data narf;

    length id $ 11 want $ 30;

    input id;
    
    want = prxchange('s/([A-Z]\d+)([A-Z]\d+)([A-Z]\d+)/$1-$2-$3/', -1, trim(id));
    
    datalines;
A123B12C123
;
run;
altijani
Quartz | Level 8

Sorry:

I need a dash on the 5th and 9th location within the variable.

andreas_lds
Jade | Level 19

As always, i thought to much 😉

Ksharp
Super User
data x;
x='A123B12C123';
want=prxchange('s/-$//',1,strip(prxchange('s/([a-z]+\d+)/$1-/i',-1,x)));

run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 4469 views
  • 2 likes
  • 5 in conversation