BookmarkSubscribeRSS Feed
ginak
Quartz | Level 8

Hi there,

 

I have a dataset with long text fields. I need to replace every instance of a dollar amount, say, "439.33" with "439.33USD"

 

Here's a sample data set:

 

 

data one;
infile cards  ;
length name $200;
input name;
cards;
Ccy="USD">738.56</NS!figlwijfoaiu345Ccy="USD">38.44</ScT@
run;

So what I have is:

 

ginak_3-1589317167406.png

 

What I want is USD to be present after every dollar amount as is here:

ginak_1-1589316860096.png

 

I found two different community forums, and know that I will need a combination of

  1. index
    1. To find the decimal point that follows any "USD" in the text
  2. substr
  3. tranwrd functions
    1. To replace that dollar amount with [dollaramount]USD

There could be multiple instances of Ccy="USD">[dollar amount] in the string. I just want to tack on the USD after the dollar amount, but not every dollar amount is XYZ.AB, some are YZ.AB, some are Z.AB etc. There will always be a decimal point with two decimal places though that follow the Ccy="USD"

 

Thanks!

 

 

6 REPLIES 6
ginak
Quartz | Level 8
Hi there, yes it'll always say "USD" (and include the double quotes in the text too). Thanks!
ChrisNZ
Tourmaline | Level 20

This replaces:

at least a digit followed by a dot followed by 2 digits 

with:

the group found followed by USD.

data TWO;
 set ONE; 
 NAME2=prxchange('s/(\d+\.\d\d)/\1USD/',-1,NAME);
run; 

{edited to avoid losing one character}

ginak
Quartz | Level 8
I think this works, thank you! It works with my fake sample data but I'll try it with my actual data tomorrow and let you know if I have any questions 🙂 I never understood the prxchange command and where it comes from. Would love to learn more since it seems so useful
ChrisNZ
Tourmaline | Level 20

The syntax is that of Perl regular expressions. 

Tons of tutorials on the web, that's how I learnt.  

 

s_lassen
Meteorite | Level 14

The solution presented by @ChrisNZ goes part of the way, but I would add a bit to make sure that only the right numbers get selected:

name=prxchange('s/("USD">\d+\.\d\d)(?=<)/\1USD/',-1,name);

This looks for the string "USD"> followed by at least one digit, a period and two digits (that's the stuff in the first paranthesis). The whole thing should be followed by "<", but that string is not included in the match (the second paranthesis, "?=" denotes a look-ahead buffer), the match is replaced with the same plus the text "USD".

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1186 views
  • 1 like
  • 3 in conversation