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

Please forgive me, I have been trying to figure this out for sometime now and I have read the various solutions related to my question, in this forum, but I am still unable to come up with a solution. 

 

My problem is I have a dataset like below (also attached) with hundreds of observations. 

 

Ft.Belvoir, VA
Annapolis NHC, MD 
Camp Pendleton NH, CA 

 

What I would like to do is keep just the name part and add the word location in the front. I would like to end up with:

 

Location Ft. Belvoir 

Location Annapolis

Location Camp Pendleton 

 

Could someone assist me with this please? 

1 ACCEPTED SOLUTION

Accepted Solutions
CurtisMackWSIPP
Lapis Lazuli | Level 10

This is made difficult because you have no clear definition of what the "name part" is.

In this example I assumed you wanted every up to the first occurrence of "NH" or the first comma.

 

data have;
  infile datalines dlm="-";
  length city $ 40;
  input city $;
datalines;
Ft.Belvoir, VA
Annapolis NHC, MD 
Camp Pendleton NH, CA 
;
run;


data want(drop=place);
  set have;
  if index(city,"NH") then place = substr(city,1,index(city,"NH")-1);
  else place = scan(city,1,", ");
  location = catx(" ","Location",place);
run;

View solution in original post

2 REPLIES 2
CurtisMackWSIPP
Lapis Lazuli | Level 10

This is made difficult because you have no clear definition of what the "name part" is.

In this example I assumed you wanted every up to the first occurrence of "NH" or the first comma.

 

data have;
  infile datalines dlm="-";
  length city $ 40;
  input city $;
datalines;
Ft.Belvoir, VA
Annapolis NHC, MD 
Camp Pendleton NH, CA 
;
run;


data want(drop=place);
  set have;
  if index(city,"NH") then place = substr(city,1,index(city,"NH")-1);
  else place = scan(city,1,", ");
  location = catx(" ","Location",place);
run;
ChrisNZ
Tourmaline | Level 20

This keeps the string until a comma or 2 uppercase letters are encountered:

data HAVE;
  input CITY :& $40.;
cards;
Ft.Belvoir, VA
Annapolis NHC, MD 
Camp Pendleton NH, CA 
run;

data WANT;
  set HAVE;
  LOCATION = prxchange('s/(.*?)(,|([A-Z]{2})).*/Location \1/',1,CITY);
run;

CITY LOCATION
Ft.Belvoir, VA Location Ft.Belvoir
Annapolis NHC, MD Location Annapolis
Camp Pendleton NH, CA Location Camp Pendleton

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 2 replies
  • 556 views
  • 4 likes
  • 3 in conversation