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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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