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

Hi, i need help in removing characters from ninth value and remain with the first 8 characters. See below dummy data

 

                     work.names

 

Data have                     Data Want

matomese@rrrtrtr         matomese

lucozade1rtrrrere           lucozade

sollyseb+rytryr               sollyseb

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @Solly7 

 

You can try this:

 

Data have ;
	input var1 $30.;
	datalines;
matomese@rrrtrtr
lucozade1rtrrrere
sollyseb+rytryr
;
run;

/* If the length of the word to extract is always 8 */
data want;
	set have;
	var2 = substr(var1,1,8);
run;

/* Otherwise you can search for the first word delimited by any character which is not in the alphabet */
data want;
	set have;
	var2 = scan(var1,1,"","ak");
run;

NB: information on modifiers for the SCAN() function (like "ak") is available on: https://documentation.sas.com/?docsetId=ds2ref&docsetTarget=p13adatt2vvhcxn1ext6w6eet24p.htm&docsetV...

 

Best,

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

Hi @Solly7 

 

You can try this:

 

Data have ;
	input var1 $30.;
	datalines;
matomese@rrrtrtr
lucozade1rtrrrere
sollyseb+rytryr
;
run;

/* If the length of the word to extract is always 8 */
data want;
	set have;
	var2 = substr(var1,1,8);
run;

/* Otherwise you can search for the first word delimited by any character which is not in the alphabet */
data want;
	set have;
	var2 = scan(var1,1,"","ak");
run;

NB: information on modifiers for the SCAN() function (like "ak") is available on: https://documentation.sas.com/?docsetId=ds2ref&docsetTarget=p13adatt2vvhcxn1ext6w6eet24p.htm&docsetV...

 

Best,

Solly7
Pyrite | Level 9

thank you so much

novinosrin
Tourmaline | Level 20

Hi @Solly7   If length of Var2 is going to be 8, all you need is define a compile time LENGTH statement and not bother with anything else .

For Example

 


Data have ;
	input var1 $30.;
	datalines;
matomese@rrrtrtr
lucozade1rtrrrere
sollyseb+rytryr
;
run;

data want;
 set have;
 length 	var2 $8;
 var2=var1;
run;

 

 

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
  • 556 views
  • 2 likes
  • 3 in conversation