BookmarkSubscribeRSS Feed
shahm
Fluorite | Level 6

Hello,

 

I have over 17000 observations in a dataset and one of the Variables is postal_code. 

I need to create a dataset which has only the correct postal codes i.e K2C0J8.

the postal code should be six digits as in the example above, have no spaces, no special characters and the 2nd, 4th and the 6th spaces should be numbers and the first, third and fifth places alphabets. 

 

here is my code so far: 

 

data CORRECTPOSTAL NOPOSTAL;
set Y;
if length(compress(postal_code,"-/+~! )( "))=6 and PRXMATCH(PRXPARSE("/\w\d\w\d\w\d/i"),compress(postal_code,"- ")) then do;
POSTAL=compress(postal_code,"-/+~! )( ");
output CORRECTPOSTAL;
end;
else output NOPOSTAL;
run;

 

this does the job alright but misses out on some observations and I dont know why. 

 

Thanks,

M

5 REPLIES 5
tarheel13
Rhodochrosite | Level 12

can you post sample data? 

ballardw
Super User

Post some examples of the values that don't return the correct result.

If you both incorrect CORRECTPOSTAL and NOPOSTAL then some of each and which result you are getting.

 

Note: It is a very good idea to post code in either a code or text box opened using the "running man" or </> icon appearing above the message window. The forum main message windows will reformat pasted text and may result in code changes, which could be pretty significant when dealing with text manipulation code.

acordes
Rhodochrosite | Level 12

you can do with one line only:

 

if prxmatch('/\b([a-zA-Z])\d([a-zA-Z])\d([a-zA-Z])\d\s/', postal_code);

a1.png

shahm
Fluorite | Level 6

Thank you all for your prompt responses! My code did identify the missing and incorrect postal codes accurately. Any ideas where can I brush up my Regular expressions. 

 

Sincerely,

M

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 5 replies
  • 654 views
  • 2 likes
  • 4 in conversation