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 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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