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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1355 views
  • 0 likes
  • 2 in conversation