BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
talisman
Fluorite | Level 6

one column named City_State_Zipcode and the data looks like this in the column

 

                                Brooklyn NY 11218

                                West Haven MN 99556

                                West Charlotte NC 12563

 

How do you separate that one column into three separate columns named City, State, Zipcode

  

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input City_State_Zipcode $30.;
datalines;
Brooklyn NY 11218       
West Haven MN 99556     
West Charlotte NC 12563 
;

data want(drop = pos length);
   set have;
   call scan(City_State_Zipcode, -2, pos, length);
   city  = substr(City_State_Zipcode, 1, pos-2);
   state = scan(City_State_Zipcode, -2);
   zip   = scan(City_State_Zipcode, -1);
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20
data have;
input City_State_Zipcode $30.;
datalines;
Brooklyn NY 11218       
West Haven MN 99556     
West Charlotte NC 12563 
;

data want(drop = pos length);
   set have;
   call scan(City_State_Zipcode, -2, pos, length);
   city  = substr(City_State_Zipcode, 1, pos-2);
   state = scan(City_State_Zipcode, -2);
   zip   = scan(City_State_Zipcode, -1);
run;
talisman
Fluorite | Level 6

one column named City_State_Zipcode and the data looks like this in the column

 

                                Brooklyn NY 11218

                                West Haven MN 99556

                                West Charlotte NC 12563

 

How do you separate that one column into three separate columns named City, State, Zipcode?

When I try to do it, West will end up in the City column and Haven will be in the State column and MN will be in the zipcode column 

Astounding
PROC Star

Try it this way:

data want;
   set have;
   zip = scan(city_state_zipcode, -1);
   state = scan(city_state_zipcode, -1);
   city = substr(city_state_zipcode, 1, length(city_state_zipcode) - 8);
run;

The final formula might need to be adjusted if it is possible that some STATE values have a length other than 2, or some zipcodes have a length other than 5.  But if this gives the answer as is, let's leave out the complexities.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1078 views
  • 0 likes
  • 4 in conversation