DATA Step, Macro, Functions and more

Count vowels from string and load in Macro variables

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Count vowels from string and load in Macro variables

Data have;

var1 var2

1 read

2 write

3 sing

4 dance

 

I want to load count of vowels from var2 to macro variable.

 

like for read - 2 vowels (o and e)

write - 2 (i and e)

goes on...

help me how to count vowels from a string.

 

Thanks

 


Accepted Solutions
Solution
‎02-02-2017 05:30 AM
Super User
Super User
Posts: 7,997

Re: Count vowels from string and load in Macro variables

[ Edited ]

Edit: I add the i modifier also to avoid casing issues - even though your test data didn't show upper case.

Use compress with the keep option and lengthn:

data want;
  set have;
  num_vowels=lengthn(compress(yourvariable,"aeiou","ki"));
run;

 

View solution in original post


All Replies
Solution
‎02-02-2017 05:30 AM
Super User
Super User
Posts: 7,997

Re: Count vowels from string and load in Macro variables

[ Edited ]

Edit: I add the i modifier also to avoid casing issues - even though your test data didn't show upper case.

Use compress with the keep option and lengthn:

data want;
  set have;
  num_vowels=lengthn(compress(yourvariable,"aeiou","ki"));
run;

 

☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 157 views
  • 0 likes
  • 2 in conversation