DATA Step, Macro, Functions and more

find the number of characters

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

find the number of characters

HI,

 

   i am wondring if exists a function to count the number of character variable,not just the times which 'a refered character' appered.

 

variable values  like '' for'' ''at'' ''in'' and so on.as I have defined the length is 500,so there exists a lot of blanks ,i may try to use the compress function or strip,but it not works out.

 

Thank you!


Accepted Solutions
Solution
‎02-07-2017 08:33 PM
PROC Star
Posts: 7,467

Re: find the number of characters

It's definitely useful to discover how the length and lengthn functions work, but I think you're going about solving your problem in a round about way. I'd use the findw function. e.g.,

 

data have;
  input @;
  name=_infile_;
cards;
iphone 6 will be launched on Nov.11 2015
ipad mini has been introduced in Japan
;

data want;
  set have;
  
  /* if you want make/model*/
  makemodel=substr(name,1,
             min(ifn(findw(name,'will',' ','s') eq 0,.,findw(name,'will',' ','s')),
                 ifn(findw(name,'has',' ','s') eq 0,.,findw(name,'has',' ','s')))-1);

  /* if you want everything before var1*/
  action=substr(name,1,
             min(ifn(findw(name,'on',' ','bs') eq 0,.,findw(name,'on',' ','bs')),
                 ifn(findw(name,'in',' ','bs') eq 0,.,findw(name,'in',' ','bs')))-1);
run;

HTH,

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,015

Re: find the number of characters

[ Edited ]

The LENGTH function, as in

   len1=length(myvar)

provides the position of the rightmost  non-blank character.

 

  len2=length(left(myvar))

does the same after left-justifying myvar.

 

And

  len3=length(compress(myvar,' ',''));

counts only the non-blanks.

Contributor
Posts: 48

Re: find the number of characters

thanks for your answers.sorry for another question
i use the length function to extract a string of characters indeed.

code like: food=substr(name,find(name,var1,'i')+length(var1),32),the variable 'name' includes the character value'var1'
but i can not get the right result.

thank you
PROC Star
Posts: 7,467

Re: find the number of characters

It would help if you provide example values of both the variables 'name' and 'var1' and what you want the value of food to be based on those values.

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 48

Re: find the number of characters

thank you!

as the 'name' variable like:

iphone 6 will be launched on Nov.11 2015

ipad mini has been introduced in Japan

 

var1 like; 

on 

in

it is just the prep in the 'name' variable.i have reverse them like'no' 'ni', var2 = reverse(var1)

 

i want to extract the 'iphone' and 'ipad mini',and i have reverse the characters for substr functon.name1 = reverse(name)

so i use the substr like 'product = substr(name1,find(name1,var2,'i')+length(var2),32)

 

athough i may not get it by one step for the other components like 'has been',

i just want to get 'iphone 6 will be launched'. and take other steps to drop the components 'will be launched'

 

thank you!

 

 

 

Contributor
Posts: 48

Re: find the number of characters

i have solved my problems 

it seems like  i have misunderstood the length of var2.

 

under some circumstances,it is better to use the trim function.

Super User
Super User
Posts: 7,942

Re: find the number of characters

Two things.  When posting a question post test data in the form of a datastep so that we can see the full picture.  

Secondly, there is a function called lengthn.  The reason for using this function is to return the number of characters of a string with trim already done.  I.e.

length(trim(<variable>))

Is the same as:

lengthn(<variable>)

 

Contributor
Posts: 48

Re: find the number of characters

ok.i have got it.thanks for your help.
Solution
‎02-07-2017 08:33 PM
PROC Star
Posts: 7,467

Re: find the number of characters

It's definitely useful to discover how the length and lengthn functions work, but I think you're going about solving your problem in a round about way. I'd use the findw function. e.g.,

 

data have;
  input @;
  name=_infile_;
cards;
iphone 6 will be launched on Nov.11 2015
ipad mini has been introduced in Japan
;

data want;
  set have;
  
  /* if you want make/model*/
  makemodel=substr(name,1,
             min(ifn(findw(name,'will',' ','s') eq 0,.,findw(name,'will',' ','s')),
                 ifn(findw(name,'has',' ','s') eq 0,.,findw(name,'has',' ','s')))-1);

  /* if you want everything before var1*/
  action=substr(name,1,
             min(ifn(findw(name,'on',' ','bs') eq 0,.,findw(name,'on',' ','bs')),
                 ifn(findw(name,'in',' ','bs') eq 0,.,findw(name,'in',' ','bs')))-1);
run;

HTH,

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 571 views
  • 0 likes
  • 4 in conversation