Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: passing indefinite no of arguments


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: passing indefinite no of arguments
Date   Sat, 31 Mar 2012 17:47:55 +0100

Frankly, this is very confused. You need to keep separate macro
manipulations and variable calculations.

Also, thisn't really a problem that needs a program or a do-file at all.

local myvars : var2 var3
unab all : var*
local rest : list all - myvars
egen rest_total = rowtotal(`rest')

I advise trying simpler problems and studying the User's manual much
more slowly and carefully.

Nick

On Sat, Mar 31, 2012 at 2:10 PM, tashi lama <[email protected]> wrote:

> That sounds great. Here is a slightly diff problem but with a similar flavor..Any idea?
>
> I have a dataset as follows
>
> var1 var2 var3 va4 var5
>
> 4     5    6     3   3
>
> I would like to enter variables as arugments and find the sum of the rest. Here is my attempt
>
> *sum of the selected vars
>
> tokenize `0'
>
> local length: word count `0'
>
> local varlist var*
>
> forvalues i=1/length {
>
>          local var_new=varlist-`i'    /* i am not so sure I can do this
>          replace varlist=`var_new'
>          drop var_new
>   }
>
>    egen total=rowtotal(`varlist')
>

> so if I run,
>
> do filename var2 var3 , I expect to get the sum of var1 var4 and var5.
>
> I am thinkin I could also do sth like this,
>
> tokenize `0'
> local length:word count `0'
>
> unab varlist:var*
>
> forvalues i=1/length {
>
>               local var_new:list varlist- `i'
>
>                 }
>
> egen total=rowtotal(`var_new')
>
> Essentially I am trying to learn to subtract certain variables from the list of variables or a string. Any help will be highly appreciated...
>
> ----------------------------------------
>> Date: Sat, 31 Mar 2012 00:48:50 +0100
>> Subject: Re: st: passing indefinite no of arguments
>> From: [email protected]
>> To: [email protected]
>>
>> di `: list sizeof 0 '
>>
>> Nick
>>
>> On Fri, Mar 30, 2012 at 8:52 PM, Maarten Buis <[email protected]> wrote:
>> > On Fri, Mar 30, 2012 at 8:43 PM, tashi lama wrote:
>> >> So, say I have a do file which will add the arguments and display
>> >>
>> >> *sum
>> >>
>> >> args a b c d
>> >>
>> >> di `a' +`b'+`c'+`d'
>> >>
>> >> This one is easy since I could pass 4 arguments a!
>  , b, c and d and my do file will sum them. What if I don't know the no of arguments I am passing? I could have passed 2, 3, 5 or even 1 and find their sum.
>> >
>> > Here is a quick solution. The logic is that within a program a number
>> > of locals are created. The local `0' contains all arguments, and the
>> > local `1' contains the first argument, `2' the second, etc. This is
>> > not very stable, e.g. when you call -tokenize- the locals `1', `2'
>> > etc. will be replaced, but in a small program like the one below it is
>> > an easy enough solution.
>> >
>> > *---------- begin example ------------
>> > program drop _all
>> > program define sumargs
>> > // count the number of arguments
>> > local k : word count!
>  `0'
>> >
>> > // add them&#13
>
> ;
>> > tempname sum
>> > scalar `sum' = 0
>> > forvalues i = 1/`k' {
>> > scalar `sum' = `sum' + ``i''
>> > }
>> >
>> > // display the result
>> > di `sum'
>> > end
>> >
>> > sumargs 2 3 7
>> > sumargs 4
>> > sumargs 2 3 4 5 6 7 8
>> > *---------- end example -------------
>> >

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index