Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of items (length) from a Stata macro?

Say I have a global macro x, how do I extract the number of items (space-separated unquoted words) in it?

global x abc xyz foo

I am sure it is an easy task, but I've looked in help macro and haven't find anything so far.

like image 772
Marcelo Avila Avatar asked Oct 24 '25 05:10

Marcelo Avila


1 Answers

Yeah, it was right there in the manual. The macro function word count would do the job.

. global x abc xyz foo
. local len_x : word count $x
. di `len_x'
3

Alternatively,

. di wordcount("$x")
3

Note the need to quote the global macro in this case.

like image 65
Marcelo Avila Avatar answered Oct 26 '25 22:10

Marcelo Avila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!