DATA Step, Macro, Functions and more

Irrespective of Length, remove numarics on end of the data

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Irrespective of Length, remove numarics on end of the data

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


Accepted Solutions
Solution
‎07-17-2014 04:37 AM
SAS Super FREQ
Posts: 682

Re: Irrespective of Length, remove numarics on end of the data

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


All Replies
Super User
Super User
Posts: 7,392

Re: Irrespective of Length, remove numarics on end of the data

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;

Contributor
Posts: 46

Re: Irrespective of Length, remove numarics on end of the data

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.

Solution
‎07-17-2014 04:37 AM
SAS Super FREQ
Posts: 682

Re: Irrespective of Length, remove numarics on end of the data

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 215 views
  • 0 likes
  • 3 in conversation