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   Wed, 4 Apr 2012 15:52:44 +0000

Hello Nick, 

 

       Could you pls look into my code below and tell me where is that I screwed although I feel like the code should work fine, at least in principle. Here is the dataset and the problem is of ranking ignoring few variables.

 

 

date           hits1     hits2     hits3      hits4   hits5

01jan2010       3          2        5          1          5  

02jan2010       5          7        8          4          2    

03jan2010       8          6        2          1          6   

04jan2010       6          5        3          2          7  

05jan2010       6          4        2          6          7  

06jan2010       1          5        3          7          8  

 

 

if "`2'"=="rank"  {

                    syntax anything                        /*allowing any data type to be passed. `3' and `4' are start_date and end_date                           
                    tokenize `anything'                          
                    local para "`1'" "`2'" "`3'" "`4'"      /* breaking arguments into 2 macros `para' and `deselected' and putting first 4 arguments into `para' and rest into `deselcted'. 
                    macro shift 4
                    local deselected "`*'" 
                       unab all : hits*
                       local varlist1 : list all - deselected                 /ignore variables from the list 
                       di "` varlist1'"                            /displays the variables after ignoring. Works until this point. 

                     rename hits* id_*                                     /changing hits1 into id_1 and so on....
                     collapse (sum) id* if tin(`3',`4')                       /summing providing date range 
                     local counter 1
                      foreach x of  varlist1 id* {                              /comparing and ranking
                          if `x'!=id_`1' & `x'>id_`1' {
                                            local  ++counter
                                                      }
                                                 }                     
                           list
                           di "There are `count' firms in total and I rank `counter' "
                     exit
                      } 

 

 

Questions:

1. When I ran

         do file 1 rank 01jan2010 02jan2010 hits3 hits4, there is an error which reads invalid syntax. I am not sure where I screwed. 

 

2. I supplied date range(01jan2010-02jan2010) for collapse sum, but it sums for all the dates (01jan2010-06jan2010). Any idea?

3. when I change the for loop as       foreach x of local varlist1  and run it, I get hits1 not found. So, it is still taking hits1 as the first variable in the varlist1. But I thougnt everything in varlist1 starts with id_ since I renamed them. Could you pls explain?

 

 Thank you very much and I really appreciate your time... 

 

 

Tashi 

 

 

 

 

 

 

 

 



----------------------------------------
&#62; Date&#58; Mon, 2 Apr 2012 23&#58;14&#58;35 &#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; It does work&#59; I edited incorrectly.&#13;&#10;&#62;&#13;&#10;&#62; . di mdy&#40;1,5,2010&#41; &#43; 1&#13;&#10;&#62; 18268&#13;&#10;&#62;&#13;&#10;&#62; On Mon, Apr 2, 2012 at 11&#58;04 PM, Nick Cox &#60;njcoxstata&#64;gmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62; Evidently, -date- is a Stata date variable, so although you see&#13;&#10;&#62; &#62; 05jan2010 it is a numeric variable and you can check the first value&#13;&#10;&#62; &#62; directly.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; . di mdy&#40;1,5,2010&#41; &#43; 1&#13;&#10;&#62; &#62; 18269&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; The program does what you asked for -- you specify some variables, you&#13;&#10;&#62; &#62; get a row total of th!
 e rest -- and also exactly what I warned you&#13;&#10;&#62; &#62; against, that if the rest are not homogeneous that will break it as&#13;&#10;&#62; &#62; far as you are concerned.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Also, a do-file does not take options.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Nick&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; On Mon, Apr 2, 2012 at 9&#58;37 PM, tashi lama &#60;ltashi32&#64;hotmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; I am sorry. I had used r&#91;&#93; instead of r&#91;&#93;. Looks like it is working although I am not sure if it is giving me the right result.&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; date var_34 var_50 var_90&#13;&#10;&#62; &#62;&#62; 05jan2010 2 1 1&#13;&#10;&#62; &#62;&#62; 06jan2010 3 2 4&#13;&#10;&#62; &#62;&#62; 07jan2010 4 8 5&#13;&#10;&#62; &#62;&#62; 08jan2010 5 4 3&#13;&#10;&#62; &#62;&#62!
 ; 09jan2010 6 3 2&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&

#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; so I invoked sumrest var_34 var_50, generate&#40;g&#41;, I got&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; date var_34 var_50 var_90 g &#124;&#13;&#10;&#62; &#62;&#62; &#124;----------------------------------------------&#124;&#13;&#10;&#62; &#62;&#62; 1. &#124; 05jan2010 2 1 1 18268 &#124;&#13;&#10;&#62; &#62;&#62; 2. &#124; 06jan2010 3 2 4 18272 &#124;&#13;&#10;&#62; &#62;&#62; 3. &#124; 07jan2010 4 8 5 18274 &#124;&#13;&#10;&#62; &#62;&#62; 4. &#124; 08jan2010 5 4 3 18273 &#124;&#13;&#10;&#62; &#62;&#62; 5. &#124; 09jan2010 6 3 2 18273 &#124;&#13;&#10;&#62; &#62;&#62; &#43;----------------------------------------------&#43;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; Any idea&#63; Also, when I run this as a do file, I get option generate&#40;&#41; not !
 allowed....&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; Thank you....&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; ----------------------------------------&#13;&#10;&#62; &#62;&#62;&#62; Date&#58; Mon, 2 Apr 2012 20&#58;32&#58;09 &#43;0100&#13;&#10;&#62; &#62;&#62;&#62; Subject&#58; Re&#58; st&#58; passing indefinite no of arguments&#13;&#10;&#62; &#62;&#62;&#62; From&#58; njcoxstata&#64;gmail.com&#13;&#10;&#62; &#62;&#62;&#62; To&#58; statalist&#64;hsphsun2.harvard.edu&#13;&#10;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62;&#62; I tested it and found two bugs. Please try this&#13;&#10;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62;&#62; program sumrest&#13;&#10;&#62; &#62;&#62;&#62; version 8.2&#13;&#10;&#62; &#62;&#62;&#62; syntax varlist&#40;numeric&#41; &#91;if&#93; &#91;in&#93; , Generate&#40;string&#41;&#13;&#10;&#62; &#62;&#62;&#62; marksample touse, novarlist&#13;&#10;&#62; &#62;&#62;&#62; qui count!
  if &#96;touse&#39;&#13;&#10;&#62; &#62;&#62;&#62; if r&#40;N&#41; &#6

1;&#61; 0 error 2000&#13;&#10;&#62; &#62;&#62;&#62; confirm new var &#96;generate&#39;&#13;&#10;&#62; &#62;&#62;&#62; unab all &#58; _all&#13;&#10;&#62; &#62;&#62;&#62; local varlist &#58; list all - varlist&#13;&#10;&#62; &#62;&#62;&#62; local varlist &#58; list varlist - touse&#13;&#10;&#62; &#62;&#62;&#62; egen &#96;generate&#39; &#61; rowtotal&#40;&#96;varlist&#39;&#41; if &#96;touse&#39;&#13;&#10;&#62; &#62;&#62;&#62; end&#13;&#10;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#38;&#35;&#33;&#13;&#10;&#62; &#62;&#62; 10&#59;&#62;&#13;&#10;&#62; &#62;&#62;&#62; On Mon, Apr 2, 2012 at 8&#58;20 PM, tashi lama &#60;ltashi32&#64;hotmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#62;&#62; &#62; Hello Nick,&#13;&#10;&#62; &#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62;&#62; &#62; I tried running the code with syntax as a do file. There was an error which reads option gener!
 ate&#40;&#41; not allowed. Any idea&#63;&#13;&#10;&#62; &#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62;&#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