DATA Step, Macro, Functions and more

Pattern matching of character variable

Reply
Occasional Contributor
Posts: 19

Pattern matching of character variable

My dataset having character variable as "PANCARD" having total 10 characters and i want to extract a data from 10 thousand records using pattern matching.
Conditions are:-
1) first 5 characters should be alphabets
2) then 4 charcters should be numeric
3) last one is alphabet.
For example.. DHPLO9635G
Occasional Contributor
Posts: 19

Re: Pattern matching of character variable

 
PROC Star
Posts: 7,471

Re: Pattern matching of character variable

You didn't say what you want to do once you found a record that matched the condition. The following will find the matching records:

 

data have;
  input pancard $10. x y;
  if prxmatch("m/[A-Z]{5}\d{4}[A-Z]/oi",pancard) > 0 then found=1;
  else found=0;
  cards;
DHPLO9635G 1 1
DH1LO9635G 0 0
;

 

HTH,

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 19

Re: Pattern matching of character variable

@art297 can we do the same without using PRXMATCH function? 

PROC Star
Posts: 7,471

Re: Pattern matching of character variable

There's often more than one possible solution to solve a given problem with SAS. In this case you could use something like:

 

data have;
  input pancard $10. x y;
  if (countc(substr(pancard,1,5),,'ai')+
    countc(substr(pancard,6,4),,'d')+
    countc(substr(pancard,10,1),,'ai')) eq 10 then found=1;
  else found=0;
  cards;
DHPLO9635G 1 1
DH1LO9635G 0 0
;

HTH,

Art, CEO, AnalystFinder.com

 

Ask a Question
Discussion stats
  • 4 replies
  • 219 views
  • 0 likes
  • 2 in conversation