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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.