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
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?
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.
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;
newId = catx("-", substr(id,1,4), substr(id,5,3), substr(id,8));
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;
Sorry:
I need a dash on the 5th and 9th location within the variable.
As always, i thought to much 😉
data x;
x='A123B12C123';
want=prxchange('s/-$//',1,strip(prxchange('s/([a-z]+\d+)/$1-/i',-1,x)));
run;
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!
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.
Ready to level-up your skills? Choose your own adventure.