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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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