Skip to content
Advertisement

How to use prxmatch or alternative for below string in sas

I have multiple rows of strings

Eg 1.
Our commission is 25% for next order
Eg2.
20% is applied for previous order

I want remove and create new column with 25% and 20% and so on… From above string.

How can I do that in SAS? The new column should flow 25% i.e percentage.

ColA.                                     ColB
Our commission is 25% for next order.     25%
20% is applied for previous order.        20%
.
.
.

So on….

Advertisement

Answer

A single percentage value (or none) can be retrieved from a string using a pattern with a grouping expression (<something>), prxmatch, and prxposn to retrieve the characters matching the grouped expression.

Example:

The percentage is presumed to be a whole number followed immediately by a percent sign.

Store the percentage as a fraction (presumed to be in 0 to 1 range) whose value is formatted for display as a percentage.

data have;
  input;
  line = _infile_;
datalines;
Eg 1.
Our commission is 25% for next order
Eg2.
20% is applied for previous order
run;

data want;
  set have;

  /* pattern for finding and capturing a whole number that is followed by a percent sign */
  rx = prxparse('/(d+)%/');

  if prxmatch(rx,line) then do;
    matched_digits = prxposn(rx, 1, line);
    fraction = input(matched_digits, 12.) / 100;
  end;

  format fraction percent5.;
run;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement