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

Hi All,

And Thank you for quick Response.

data have;

input error $200.;

cards;

1 LLLL MMMMM NNNN Missing0048165

2 LLLL MMMMM NNNN Missing

3 AAAAAA BBBBB CCCCCCCC segment Required0048158

4 AAAAAA BBBBB CCCCCCCC segment Required

5 XXXX YYYYYY ZZZZZZ (Required) Missing0000047

6 XXXX YYYYYY ZZZZZZ (Required) Missing

;

run;

From here -  if data is ending with number need to remove the number

(if number is available on end of the data its not same and having differnt length)

output like

1 LLLL MMMMM NNNN Missing

2 LLLL MMMMM NNNN Missing

3 AAAAAA BBBBB CCCCCCCC segment Required

4 AAAAAA BBBBB CCCCCCCC segment Required

5 XXXX YYYYYY ZZZZZZ (Required) Missing

6 XXXX YYYYYY ZZZZZZ (Required) Missing

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

I would go for a regular expression, see below:

The \d idenifies digits, the + matches one or more times, the $ matches the position at the end of the input string

It is important to trim the string first

data have;
  infile cards truncover;
 
input error $200.;
 
cards;
1 LLLL MMMMM NNNN Missing0048165a
2 LLLL MMMMM NNNN Missing
3 AAAAAA BBBBB CCCCCCCC segment Required0048158
4 AAAAAA BBBBB CCCCCCCC segment Required
5 XXXX YYYYYY ZZZZZZ (Required) Missing0000047
6 XXXX YYYYYY ZZZZZZ (Required) Missing
;

data want;
  set have;
  digitStart = prxmatch("/\d+$/", trim(error));
  part1 = substr(error, 1, digitStart-1);
  part2 = substr(error, digitStart);
run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Well, there are several options here:

Perl Regular expressions - This is probably the main options for string matching/replacing, however I foget the exact terminology now (prxparse, prxmatch etc)

Substr:  Assuming the first number is zero (or a pattern) then you can do: substr(text,1,index(text,"0")-1);

String array: Just for fun this one:

data want;

     set have;

     do I=1 to length(text);

          if substr(text,i,1) in ('0','1','2',...) then substr(text,i,1)=" ";

     end;

run;

sas_lak
Quartz | Level 8

Thank u RW9,

But small correction from your code

I dont want remove any numbers from the data, which is, data ending with numbers like

3 AAAAAA BBBBB CCCCCCCC segment Required0048158   need to remove 0048158 only.(not any numbers from middle of the data)

Could you please help me to get it.

BrunoMueller
SAS Super FREQ

I would go for a regular expression, see below:

The \d idenifies digits, the + matches one or more times, the $ matches the position at the end of the input string

It is important to trim the string first

data have;
  infile cards truncover;
 
input error $200.;
 
cards;
1 LLLL MMMMM NNNN Missing0048165a
2 LLLL MMMMM NNNN Missing
3 AAAAAA BBBBB CCCCCCCC segment Required0048158
4 AAAAAA BBBBB CCCCCCCC segment Required
5 XXXX YYYYYY ZZZZZZ (Required) Missing0000047
6 XXXX YYYYYY ZZZZZZ (Required) Missing
;

data want;
  set have;
  digitStart = prxmatch("/\d+$/", trim(error));
  part1 = substr(error, 1, digitStart-1);
  part2 = substr(error, digitStart);
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1577 views
  • 0 likes
  • 3 in conversation