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?
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;
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;
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 |
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.