BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hello,

I use prematch function below.   It seems like my program wasn't working.  How to fix it?  Thanks.

 

data datain;

format name $ 500.;

input name &;

cards;

CAR ID 5648MA

0009463

000-4721

;

run;

data dataout;
  set datain;
  if prxmatch("m/(?=.*?If_True)(?=.*?5648)/i",name) > 0 then found=1;
  else if prxmatch("m/(?=.*?If_True)(?=.*?9463)/i",name) > 0 then found=2;
  else if prxmatch("m/(?=.*?If_False)(?=.*?4721)/i",name) > 0 then found=3;
  else found=0;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

Looks like you based your code on another Communities answer where If_True and If_False wereactually part of the data. They are not part of any regex syntax. Here they are not so you will always see found=0. I have simplified your regex and this works for me. I hope it also work for you.

 

data dataout;
  set datain;
  if prxmatch("m/.*?5648/i",name) > 0 then found=1;
  else if prxmatch("m/.*?9463/i",name) > 0 then found=2;
  else if prxmatch("m/.*?4721/i",name) > 0 then found=3;
  else found=0;
run;

 

Regards,

- Jan.

 

 

View solution in original post

2 REPLIES 2
jklaverstijn
Rhodochrosite | Level 12

Looks like you based your code on another Communities answer where If_True and If_False wereactually part of the data. They are not part of any regex syntax. Here they are not so you will always see found=0. I have simplified your regex and this works for me. I hope it also work for you.

 

data dataout;
  set datain;
  if prxmatch("m/.*?5648/i",name) > 0 then found=1;
  else if prxmatch("m/.*?9463/i",name) > 0 then found=2;
  else if prxmatch("m/.*?4721/i",name) > 0 then found=3;
  else found=0;
run;

 

Regards,

- Jan.

 

 

ed_sas_member
Meteorite | Level 14

Hi @ybz12003 

 

A simplier approach, assuming that the condition to flag the variable is to find the pattern anywhere in the string (no condition such as  the string ends with the pattern, ...)

data dataout;
  set datain;
  if prxmatch("/5648/",name) > 0 then found=1;
  else if prxmatch("/9463/",name) > 0 then found=2;
  else if prxmatch("/4721/",name) > 0 then found=3;
  else found=0;
run;

-> no need to add the "i" modifier as you look for digits and not letters

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
  • 2 replies
  • 741 views
  • 0 likes
  • 3 in conversation