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 (.*?)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.