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

So I have a variable in my dataset that combines letters and numbers without a defined pattern.

 

Something like:

 

ID              Name

1                G1CA

2                ST10C

3                Unit8

4                10A

 

 

I'd like to parse out the characters and numbers in the strings within the groupings which they occur so for ID #1 I'd like it to provide 3 additional columns, Col1 containing "G", Col2 containing 1, and Col3 containing "CA".

 

I've been looking into the "ANYALPHA" and "NOTALPHA" which returns the first position of the character or number which I could then possibly combine with a substr function?

 

I am hoping there is something a little more straightforward?

 

Thanks in advance for your help!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simplest way to get only letters or only numbers is by using compress with the appropriate options:

data want;
  array c{20} $1;
  array n{20} 8;
  str="G1CA";
  chars=compress(str," ","ka");
  nums=compress(str," ","kd");
  do i=1 to lengthn(chars);
    c{i}=char(chars,i);
  end;
  do i=1 to lengthn(nums);
    n{i}=char(nums,i);
  end;
run;

That will split out each alpha to c{*} array and each number to n{*} - note max 20 of each (I don't know how long for maximum so went for 20).  

 

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simplest way to get only letters or only numbers is by using compress with the appropriate options:

data want;
  array c{20} $1;
  array n{20} 8;
  str="G1CA";
  chars=compress(str," ","ka");
  nums=compress(str," ","kd");
  do i=1 to lengthn(chars);
    c{i}=char(chars,i);
  end;
  do i=1 to lengthn(nums);
    n{i}=char(nums,i);
  end;
run;

That will split out each alpha to c{*} array and each number to n{*} - note max 20 of each (I don't know how long for maximum so went for 20).  

 

xezus
Calcite | Level 5

That works perfectly! I can play around with concatenating the characters into strings to get all possible combinations, luckily my max name length is only 6.

 

Thank you so much!

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
  • 2 replies
  • 1872 views
  • 0 likes
  • 2 in conversation