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   tashi lama <[email protected]>
To   <[email protected]>
Subject   RE: st: passing indefinite no of arguments
Date   Sat, 31 Mar 2012 13:10:31 +0000

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

 

Thanx, 

Tashi

 

 

  

----------------------------------------
&#62; Date&#58; Sat, 31 Mar 2012 00&#58;48&#58;50 &#43;0100&#13;&#10;&#62; Subject&#58; Re&#58; st&#58; passing indefinite no of arguments&#13;&#10;&#62; From&#58; njcoxstata&#64;gmail.com&#13;&#10;&#62; To&#58; statalist&#64;hsphsun2.harvard.edu&#13;&#10;&#62;&#13;&#10;&#62; di &#96;&#58; list sizeof 0 &#39;&#13;&#10;&#62;&#13;&#10;&#62; Nick&#13;&#10;&#62;&#13;&#10;&#62; On Fri, Mar 30, 2012 at 8&#58;52 PM, Maarten Buis &#60;maartenlbuis&#64;gmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62; On Fri, Mar 30, 2012 at 8&#58;43 PM, tashi lama wrote&#58;&#13;&#10;&#62; &#62;&#62; So, say I have a do file which will add the arguments and display&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; &#42;sum&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; args a b c d&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; di &#96;a&#39; &#43;&#96;b&#39;&#43;&#96;c&#39;&#43;&#96;d&#39;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; 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&#39;t know the no of arguments I am passing&#63; I could have passed 2, 3, 5 or even 1 and find their sum.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Here is a quick solution. The logic is that within a program a number&#13;&#10;&#62; &#62; of locals are created. The local &#96;0&#39; contains all arguments, and the&#13;&#10;&#62; &#62; local &#96;1&#39; contains the first argument, &#96;2&#39; the second, etc. This is&#13;&#10;&#62; &#62; not very stable, e.g. when you call -tokenize- the locals &#96;1&#39;, &#96;2&#39;&#13;&#10;&#62; &#62; etc. will be replaced, but in a small program like the one below it is&#13;&#10;&#62; &#62; an easy enough solution.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; &#42;---------- begin example ------------&#13;&#10;&#62; &#62; program drop _all&#13;&#10;&#62; &#62; program define sumargs&#13;&#10;&#62; &#62; // count the number of arguments&#13;&#10;&#62; &#62; local k &#58; word count!
  &#96;0&#39;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; // add them&#13

;&#10;&#62; &#62; tempname sum&#13;&#10;&#62; &#62; scalar &#96;sum&#39; &#61; 0&#13;&#10;&#62; &#62; forvalues i &#61; 1/&#96;k&#39; &#123;&#13;&#10;&#62; &#62; scalar &#96;sum&#39; &#61; &#96;sum&#39; &#43; &#96;&#96;i&#39;&#39;&#13;&#10;&#62; &#62; &#125;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; // display the result&#13;&#10;&#62; &#62; di &#96;sum&#39;&#13;&#10;&#62; &#62; end&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; sumargs 2 3 7&#13;&#10;&#62; &#62; sumargs 4&#13;&#10;&#62; &#62; sumargs 2 3 4 5 6 7 8&#13;&#10;&#62; &#62; &#42;---------- end example -------------&#13;&#10;&#62; &#62;&#13;&#10;&#62;&#13;&#10;&#62; &#42;&#13;&#10;&#62; &#42; For searches and help try&#58;&#13;&#10;&#62; &#42; http&#58;//www.stata.com/help.cgi&#63;search&#13;&#10;&#62; &#42; http&#58;//www.stata.com/support/statalist/faq&#13;&#10;&#62; &#42; http&#58;//www.ats.ucla.edu/stat/stata/&#13;&#10; 		 	   		  
*
*   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