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

Hello,

 

Can anyone help me with how to parse dollar amounts from a Text variable in Base SAS? The challenge is the textg string doesn't have uniform formats, like $100.50 could be entered as 100.50 or even $ 100.50. The only constant would be '.XX' with varying number of characters to the left of the decimal point. Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This should be able to deal with all kinds of circumstances:

data want (drop=i i1 field);
set have;
length field $20;
i = findc(instring,'.',1);
do while (i ne 0);
  if notdigit(substr(instring,i+1,2)) = 0
  then do;
    i1 = i - 1;
    do while (notdigit(substr(instring,i1,1)) = 0);
      i1 + (-1);
    end;
    field = substr(instring,i1+1,i+2-i1);
    amount = input(field,20.2);
    i = 0;
  end;
  else i = findc(instring,'.',i+1);
end;
run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Remove the $ with compress statement and then convert to a numeric variable with an input function. 

Conundrum
Calcite | Level 5

I forgot to add this is a comment field where there is more than just the dollar amount in the text field, it will have for instance, 'amount of $100.50 entered on MM/DD/YYYY due to xyz'. I want to create a new variable that just has the amount of 100.50 for this example. Will this solution work for this scenario?

Reeza
Super User

No, that's a big difference.

 

Post several of the variations that you have, preferably one of each. The best solution will be a PRX but I'm not good at that.

Kurt_Bremser
Super User

This should be able to deal with all kinds of circumstances:

data want (drop=i i1 field);
set have;
length field $20;
i = findc(instring,'.',1);
do while (i ne 0);
  if notdigit(substr(instring,i+1,2)) = 0
  then do;
    i1 = i - 1;
    do while (notdigit(substr(instring,i1,1)) = 0);
      i1 + (-1);
    end;
    field = substr(instring,i1+1,i+2-i1);
    amount = input(field,20.2);
    i = 0;
  end;
  else i = findc(instring,'.',i+1);
end;
run;
Conundrum
Calcite | Level 5

This worked great and exactly what I needed. I will tweak this code for the instances where there was no decimal point and only had like '$20' for the amount.

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
  • 5 replies
  • 1457 views
  • 1 like
  • 3 in conversation