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!
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
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.
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
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!
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.
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>)
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
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!
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.
Ready to level-up your skills? Choose your own adventure.