BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Quodly
Obsidian | Level 7
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?

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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.

View solution in original post

5 REPLIES 5
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
s_lassen
Meteorite | Level 14

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.

Quodly
Obsidian | Level 7
great, thanks! with that i can still use those look expressions.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1207 views
  • 3 likes
  • 4 in conversation