There is a column that has city and state together. How to separate them?I used scan function.But there are many ways in which the data is stored as shown below:
EAST TEMPLETO MA
ST. PETE FL
One thing that is definete is , scan(variable_name,-1,'') is giving me state, but the issue is with City,
Can you assume that everything except the last word (state) is the city?
Yes
Then all you need to know if the column number of the character (blank) that is just before the last word. Maybe ANYBLANK with negative start. Is any blank a function?
Another hint. RTM about the CALL version of SCAN.
You're saying you can extract STATE. Then it should be easy enough to get CITY:
city = substr(original_variable, 1, length(original_variable) - length(state));
Also note that this code gives CITY a length of $200. You may want to set a shorter length before adding this statement. If the length of CITY might vary from data set to data set, you could always try this first:
city = original_variable;
Any advice from data _null_ that inclues RTFM is best heeded. A more complex problem would require better tools than SUBSTR.
Good luck.
when I run this, i get an error invalid thrid argument for substr
Can you provide an example where you get such an error using data_null_'s suggested code? I can only imagine that happening if you have a blank line, or only a city or state, but you've already stated that state will always be the last text.
Thats true.
show your work
Assuming the states are always two character the following codes would help(tested correct).
Otherwise look for the position of the spaces.
data have1;
set have;
city_state1=compbl(city_state||'*');
run;
data want;
set have1;
j=index(city_state1,'*');
state=substr(city_state1,j-3,j-2);
city=substr(city_state1,1,j-4);
state=tranwrd(state,'*','');
drop city_state1 j;
run;
city_state | city_state1 | j | state | city |
Montgomery AL | Montgomery AL * | 15 | AL | Montgomery |
Juneau AK | Juneau AK * | 11 | AK | Juneau |
Phoenix AZ | Phoenix AZ * | 12 | AZ | Phoenix |
Little Rock AR | Little Rock AR * | 16 | AR | Little Rock |
Sacramento CA | Sacramento CA * | 15 | CA | Sacramento |
Denver CO | Denver CO * | 11 | CO | Denver |
Hartford CT | Hartford CT * | 13 | CT | Hartford |
Dover DE | Dover DE * | 10 | DE | Dover |
Tallahassee FL | Tallahassee FL * | 16 | FL | Tallahassee |
Hi,
Here is my try...Might be it will same as others' logic...
data have(drop = position length);
input test_var $50.;
length state $5. city $20.;
state = scan(test_var,-1," ");
call scan(test_var,-1,position,length);
city = substr(test_var,1,(position-1));
cards4;
EAST TEMPLETO MA
ST. PETE FL
;;;;
-Urvish
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.