Hi Folks:
I'm trying to use the beginning part of the string variable (i.e., Albany- or Albany(county) or Albany county) until any of these symbols happen as part of the string: '-', ' ' , '('
The code below does the job but too inefficient.
Is there a way to accomplish this in a single step?
data TEMP; set census_age1;
IDNAME1=substr(IDNAME,1,index(IDNAME,'-')-1);
DROP IDNAME; RENAME IDNAME1=IDNAME;
ID1NAME1=substr(ID1NAME,1,index(ID1NAME,'-')-1);
DROP ID1NAME; RENAME ID1NAME1=ID1NAME;
RUN;
DATA TEMP1; SET TEMP;
IDNAME1=substr(IDNAME,1,index(IDNAME,' ')-1);
DROP IDNAME; RENAME IDNAME1=IDNAME;
RUN;
DATA census_age2; SET TEMP1;
IDNAME1=substr(IDNAME,1,index(IDNAME,'(')-1);
DROP IDNAME; RENAME IDNAME1=IDNAME;
run;
You can try scan:
data have;
input idname $20.;
datalines;
Albany-
Albany(county)
Albany (county)
Albany
;
run;
data want(rename=new_idname=idname);
set have;
new_idname=scan(idname, 1, '(- ');
drop idname;
run;
@Cruise wrote:
Hi Folks:
I'm trying to use the beginning part of the string variable (i.e., Albany- or Albany(county) or Albany county) until any of these symbols happen as part of the string: '-', ' ' , '('
The code below does the job but too inefficient.
Is there a way to accomplish this in a single step?
You don't say what the desired output is. You don't provide (a portion of) the input data set. We need those things if we're going to help you.
You can try scan:
data have;
input idname $20.;
datalines;
Albany-
Albany(county)
Albany (county)
Albany
;
run;
data want(rename=new_idname=idname);
set have;
new_idname=scan(idname, 1, '(- ');
drop idname;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.