BookmarkSubscribeRSS Feed
aasdfafafsdfsaf
Calcite | Level 5
ah i was understanding it wrong. have a follow up question. How would i modify this portion of the regex to include future possible changes to ID for example like looking for text such as USERID or UID, personID with 9 or 10 digit numbers? '/\bid[: ]+(\d([\d- ]){6,10}\d)\b/oi'
Patrick
Opal | Level 21

@aasdfafafsdfsaf wrote:
ah i was understanding it wrong. have a follow up question. How would i modify this portion of the regex to include future possible changes to ID for example like looking for text such as USERID or UID, personID with 9 or 10 digit numbers? '/\bid[: ]+(\d([\d- ]){6,10}\d)\b/oi'

Something like below should work

'/\b(person|user|u)?id[: ]+(\d([\d- ]){9,12}\d)\b/oi'

With RegEx details matter and it's imho best to always test them with sample data that include all your desired patterns and ideally also the ones that are close but you don't want selected - like: a blank between person and id.

 

For ([\d- ]){9,12}: If you want to allow for multiple blanks then the upper boundary must be >10 

 

If there shouldn't be any blanks between the digits then the RegEx would need to look like:

'/\b(person|user|u)?id[: ]+(\d{9,10}\d)\b/oi'
s_lassen
Meteorite | Level 14

Something like this may do the trick:

data want;
  set have;
  length ID $8;
  _N_=findw(str,'ID',': .');
  if _N_ then 
    ID=compress(substr(str,_N_,14),,'DK');
run;

The parameters for the COMPRESS function simply deletes anything that is not digits, and because ID has a length of 8, only the first 8 digits are included. You may have to modify the parameters for the FINDW function to find all occurrences of "ID" (the parameters shown are the ones needed for the example data provided by @Patrick ). The third parameter (14) to SUBSTR is just a guess, we do not want to scan the whole rest of the string in case somebody entered an ID with only 7 digits in it (in which case the "14" should more or less guarantee that we do not get the first digit of the ZIP code or the date added to the ID).

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 17 replies
  • 1693 views
  • 2 likes
  • 6 in conversation