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!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1606 views
  • 1 like
  • 3 in conversation