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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1869 views
  • 2 likes
  • 4 in conversation