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
Wed, 4 Apr 2012 20:23:02 +0100
No, sorry, that was quite wrong in part.
Local macro 0 _is_ defined automatically as all the arguments to a
do-file. However, in this case the two lines
syntax anything
tokenize `anything'
don't do anything that can't be done in terms of macros `1' `2' `3' `4' up.
On Wed, Apr 4, 2012 at 6:54 PM, Nick Cox <[email protected]> wrote:
> Tashi:
>
> It is best to think that you are addressing Statalist, not any one person.
>
> Evidently you have a do-file -file.do- but where it starts and ends is
> not clear from your code fragments below.
>
> Evidently you are using -syntax- in a do-file which is not illegal but
> it is unusual. You need to understand that -syntax- looks inside the
> local macro 0, which apparently you never define. In a program,
> defined by code following a -program- statement, that is done
> automatically. However, `1' `2' etc. are defined in a do-file so your
> misunderstanding does not bite you. That is, the two lines
>
> syntax anything
> tokenize `anything'
>
> do absolutely nothing but that does not invalidate your problem. On
> the other hand,
>
> foreach x of varlist1 id* {
>
> is illegal. You are confusing two quite different syntaxes: examples are.
>
> foreach x of local varlist1 {
>
> foreach x of varlist id* {
>
> As I have tried to signal before, I advise against trying to write
> your own ranking code when Stata (and Mata) provide tools for sorting
> and ranking and there are existing programs to be exploited.
>
> Nick
>
> On Wed, Apr 4, 2012 at 4:52 PM, tashi lama <[email protected]> wrote:
>> 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...
>>
*
* 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/