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