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

 

 

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2465 views
  • 4 likes
  • 3 in conversation