DATA Step, Macro, Functions and more

Help with extracting first few character of a string

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Help with extracting first few character of a string

Hello Everyone,

 

I have text variable and I want to create new variable (new_char_var) that only keep the first, say 5, character.

So the new variable will have value: 

vari1

abcdv

Can you help me to get it?

I try to do substring but doesnt work.

Thank you so much.

HHC

 

data have;
input  var1 $;
datalines;
vari1234
abcdv123
;run;

Accepted Solutions
Solution
‎04-26-2017 10:30 AM
Respected Advisor
Posts: 4,649

Re: Help with extracting first few character of a string

substr() does work...

 

data have;
input  var1 $;
newValue = substr(var1,1,5);
datalines;
vari1234
abcdv123
;
PG

View solution in original post


All Replies
Solution
‎04-26-2017 10:30 AM
Respected Advisor
Posts: 4,649

Re: Help with extracting first few character of a string

substr() does work...

 

data have;
input  var1 $;
newValue = substr(var1,1,5);
datalines;
vari1234
abcdv123
;
PG
Super User
Posts: 5,082

Re: Help with extracting first few character of a string

While SUBSTR does work, it isn't needed when you want only the beginning of a character string:

 

data want;

set have;

length new_char_var $ 5;

new_char_var = var1;

run;

Super User
Posts: 6,938

Re: Help with extracting first few character of a string

Show your code (and log) where substr "didn't work".


hhchenfx wrote:

Hello Everyone,

 

I have text variable and I want to create new variable (new_char_var) that only keep the first, say 5, character.

So the new variable will have value: 

vari1

abcdv

Can you help me to get it?

I try to do substring but doesnt work.

Thank you so much.

HHC

 

data have;
input  var1 $;
datalines;
vari1234
abcdv123
;run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 9,681

Re: Help with extracting first few character of a string

If I understood what you mean.

 

data have;
input  var1 $;
new=scan(var1,1, ,'ka');
datalines;
vari1234
abcdv123
;
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 172 views
  • 4 likes
  • 5 in conversation