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

Hi SAS Forum,
I have a list of some 200 postal codes in the attached SAS data set as a string.

Q:

I need to get the postal codes separated. The final data set that I want should look like below.

postal_code

T1S0C3

T1S1A1

T1S1A2

T1S1A4

T1S1A5

.......

.......

.......

Could anyone help me to get the single string transformed into a data set like above?

Thank you for your time and expertise.

Mirisage

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Have a look at this code

COUNTW will count the "words" delimited by a blank, SCAN will get the proper string

data postalcodes;
  postal_codes = "T1S0C3 T1S1A1 T1S1A2 T1S1A4 T1S1A5 T1S1A6 T1S1A7 T1S1A8 T1S1A9 T1S1B1 T1S1B2";
 
length postalCode $ 16;
  n_postalCodes = countw(postal_codes,
" ");
  do i = 1 to n_postalCodes;
    postalCode = scan(postal_codes, i, " ");
    output;
 
end;

 
drop postal_codes /* n_postalCodes i */;
run;
 

View solution in original post

3 REPLIES 3
Haikuo
Onyx | Level 15

Please refer this thread for your answer:

https://communities.sas.com/thread/48092

BrunoMueller
SAS Super FREQ

Have a look at this code

COUNTW will count the "words" delimited by a blank, SCAN will get the proper string

data postalcodes;
  postal_codes = "T1S0C3 T1S1A1 T1S1A2 T1S1A4 T1S1A5 T1S1A6 T1S1A7 T1S1A8 T1S1A9 T1S1B1 T1S1B2";
 
length postalCode $ 16;
  n_postalCodes = countw(postal_codes,
" ");
  do i = 1 to n_postalCodes;
    postalCode = scan(postal_codes, i, " ");
    output;
 
end;

 
drop postal_codes /* n_postalCodes i */;
run;
 
Mirisage
Obsidian | Level 7

Hi Bruno

Thank you very much, your code worked nicely and explanation is very helpful.

Hi Hai.kuo,

Mnay thanks for your link which is very helpful.

Regards

Mirisage

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1404 views
  • 3 likes
  • 3 in conversation