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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

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

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
unison
Lapis Lazuli | Level 10

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;
-unison
Cruise
Ammonite | Level 13
Thanks, solved the problem. Sorry for not providing a mock data. Thanks again!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1640 views
  • 1 like
  • 3 in conversation