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
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;
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;
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
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.