After a bit of experimenting with the PRX - INFORMAT technique, starting with 's third example, I came to :
proc format;
invalue ReDollar (default=32767)
's#.*\$\s?(\d+(\.\d{2})?).*#\1#' (regexpe) = [12.];
run;
data want3;
set have;
price=input(pt, ReDollar.);
format price dollar8.2;
run;
I dropped the non-capturing group and the begin-end assertions from the pattern since they are not required. I changed the label informat dollar8.2 to 12. as it divided the price by 100. Most importantly, I specified a large default size for the informat; otherwise, longer input strings were truncated.
PG
It is important to note that in the case where a variable this format would be applied to includes more that one valid dollar value, the last one will be returned. This is true of all the examples: mine, ,
This is because of the use of the greedy match at the beginning (.*), if you alternatively wanted to retrieve the first match, you could modify this to be a lazy match (.*?)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.