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
----------------------------------------
> Date: Mon, 2 Apr 2012 23:14:35 +0100 > Subject: Re: st: passing indefinite no of arguments > From: njcoxstata@gmail.com > To: statalist@hsphsun2.harvard.edu > > It does work; I edited incorrectly. > > . di mdy(1,5,2010) + 1 > 18268 > > On Mon, Apr 2, 2012 at 11:04 PM, Nick Cox <njcoxstata@gmail.com> wrote: > > Evidently, -date- is a Stata date variable, so although you see > > 05jan2010 it is a numeric variable and you can check the first value > > directly. > > > > . di mdy(1,5,2010) + 1 > > 18269 > > > > The program does what you asked for -- you specify some variables, you > > get a row total of th!
e rest -- and also exactly what I warned you > > against, that if the rest are not homogeneous that will break it as > > far as you are concerned. > > > > Also, a do-file does not take options. > > > > Nick > > > > On Mon, Apr 2, 2012 at 9:37 PM, tashi lama <ltashi32@hotmail.com> wrote: > >> > >> > >> I am sorry. I had used r[] instead of r[]. Looks like it is working although I am not sure if it is giving me the right result. > >> > >> > >> > >> date var_34 var_50 var_90 > >> 05jan2010 2 1 1 > >> 06jan2010 3 2 4 > >> 07jan2010 4 8 5 > >> 08jan2010 5 4 3 > >>!
; 09jan2010 6 3 2 > >> > >>&
#13; > >> > >> > >> > >> so I invoked sumrest var_34 var_50, generate(g), I got > >> > >> > >> > >> date var_34 var_50 var_90 g | > >> |----------------------------------------------| > >> 1. | 05jan2010 2 1 1 18268 | > >> 2. | 06jan2010 3 2 4 18272 | > >> 3. | 07jan2010 4 8 5 18274 | > >> 4. | 08jan2010 5 4 3 18273 | > >> 5. | 09jan2010 6 3 2 18273 | > >> +----------------------------------------------+ > >> > >> > >> > >> Any idea? Also, when I run this as a do file, I get option generate() not !
allowed.... > >> > >> > >> > >> Thank you.... > >> > >> ---------------------------------------- > >>> Date: Mon, 2 Apr 2012 20:32:09 +0100 > >>> Subject: Re: st: passing indefinite no of arguments > >>> From: njcoxstata@gmail.com > >>> To: statalist@hsphsun2.harvard.edu > >>> > >>> I tested it and found two bugs. Please try this > >>> > >>> program sumrest > >>> version 8.2 > >>> syntax varlist(numeric) [if] [in] , Generate(string) > >>> marksample touse, novarlist > >>> qui count!
if `touse' > >>> if r(N) 
1;= 0 error 2000 > >>> confirm new var `generate' > >>> unab all : _all > >>> local varlist : list all - varlist > >>> local varlist : list varlist - touse > >>> egen `generate' = rowtotal(`varlist') if `touse' > >>> end > >>> > >>> > >> &#! > >> 10;> > >>> On Mon, Apr 2, 2012 at 8:20 PM, tashi lama <ltashi32@hotmail.com> wrote: > >>> > Hello Nick, > >>> > > >>> > > >>> > > >>> > I tried running the code with syntax as a do file. There was an error which reads option gener!
ate() not allowed. Any idea? > >>> > > >>> > > >>> > > > * > * 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/
*
* 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/