Solved
Contributor
Posts: 48

# 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: 8,169

## 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

All Replies
Posts: 1,345

## 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

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: 8,169

## 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
Posts: 9,599

## 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: 8,169

## 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.