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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

8 REPLIES 8
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
JNWong
Fluorite | Level 6
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
art297
Opal | Level 21

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

 

JNWong
Fluorite | Level 6

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!

 

 

 

JNWong
Fluorite | Level 6

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

 

JNWong
Fluorite | Level 6
ok.i have got it.thanks for your help.
art297
Opal | Level 21

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

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 14840 views
  • 1 like
  • 4 in conversation