Calcite | Level 5

## Pulling all characters/numbers out of a string

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Pulling all characters/numbers out of a string

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).

2 REPLIES 2
Diamond | Level 26

## Re: Pulling all characters/numbers out of a string

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).

Calcite | Level 5

## Re: Pulling all characters/numbers out of a string

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!

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