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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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