data a;
txt = "Name: Alfred"||'0D'x||"Age: "||'0D'x||"Height: 69";
pat = prxparse("/(?<=Age: ).*(?=\r)/"); /* this does not work */
call prxsubstr(pat, txt, beg, len);
if beg then age = substr(txt, beg, len);
output;
pat = prxparse("/(?<=Age: )[^\r]*(?=\r)/"); /* this neither */
call prxsubstr(pat, txt, beg, len);
if beg then age = substr(txt, beg, len);
output;
run;instead of the age, the text until the next carriage return is selected. how can i assign the empty string without changing the look expressions?
The problem is not with PRX, it is with using a length of zero in a call to the SUBSTR function - the LEN variable is zero, but the SUBSTR function does not accept that - you get a note about "Invalid third argument to function SUBSTR" in both your calls.
Use the SUBSTRN function instead, that will correctly return an empty string.
Hi,
how about that:
data have;
txt = "Name: Alfred"||'0D'x||"Age: 34"||'0D'x||"Height: 69"; output;
txt = "Name: Anna"||'0D'x||"Age: "||'0D'x||"Height: 69"; output;
run;
data a;
set have;
pat = prxparse("/(\bAge: ).*(\r)/");
call prxsubstr(pat, txt, beg, len);
if beg then age = scan(substr(txt, beg, len),2,":");
output;
run;All the best
Bart
Alternatively could try the prxchange
data have;
txt = "Name: Alfred"||'0D'x||"Age: 34"||'0D'x||"Height: 69"; output;
txt = "Name: Anna"||'0D'x||"Age: "||'0D'x||"Height: 69"; output;
run;
data a;
set have;
age=prxchange('s/(.*age:)(.*)(\sheight.*)/$2/oi',-1,txt);
run;
With your example
data a;
txt = "Name: Alfred"||'0D'x||"Age: "||'0D'x||"Height: 69";
age=prxchange('s/(.*age:)(.*)(\sheight.*)/$2/oi',-1,txt);
run;
The problem is not with PRX, it is with using a length of zero in a call to the SUBSTR function - the LEN variable is zero, but the SUBSTR function does not accept that - you get a note about "Invalid third argument to function SUBSTR" in both your calls.
Use the SUBSTRN function instead, that will correctly return an empty string.
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.