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   Mon, 2 Apr 2012 18:57:19 +0100

Stata has its own terminology. A program, strict sense, is defined by
a -program- command. What you have below is a do-file or part of it.
That's fine. Many Stata users write do-files all the time, and only
do-files, and that's fine.

-syntax-, -marksample- and -touse- are all basic Stata programming
commands, which in practice are used mostly with programs as defined
strictly. They are well documented. Nothing obliges you to write your
own programs in Stata's sense. I showed you how one fairly experienced
user, namely me, would approach your last problem, but there is
nothing compulsory about what I did.

However, I don't see anything new in this posting. Techniques for
omitting variables from lists of variables have been covered
repeatedly in the threads that you have started. References have been
given to programs already written to do part of what you want. You
seem determined to write your own code. I respect that but I don't
have anything to add to what I have tried to explain in several posts.

Nick

On Mon, Apr 2, 2012 at 5:14 PM, tashi lama <[email protected]> wrote:
>
> Hello Nick,
>
>           Thank you. I tried reading/running your code that uses syntax. I will be frank with you, I couldn't follow your code even after spending quite some time trying to follow. The problem I think is due to too many new keywords i.e. syntax, marksample and touse. I looked for them in manual and still not sure. I am posting the similar question in a little diff way. I am hoping that might break the ice.
>
>
>
> date                       var1       var2      var3       var4     var5    var6
>
> 01jan2010                   1          3          5          2        1        6
>
> 02jan2010                   6          2          1          5        6        7
>
> 03jan2010                   4          2          5          7        8        1
>
>
> This was my program to rank and it works...
>
>
> clear
> args firm_id start_date end_date graph_type
> if "`graph_type'"=="rank" {
>                      collapse (sum) var* if tin(`start_date',`end_date')
>                      local counter 1
>       foreach x of varlist var* {
>                          if `x'!=`firm_id' & `x'>`firm_id' {
>                                            local  ++counter
>                                           }
>                                        }
>                   list
>                   di " I rank `counter' "
>                   exit
>                 }
>
>
> The problem is I want to be able to ignore certain variables and I want to be able to list those variables as arguments preferably after my 4 initial arguments. So, if I want to exclude var5, then
>
>
> do filename var4 01jan2010 03jan2010 rank var5       (Note: I want to have liberty to enter any no of variables to be ignored in my arguments....)
>
>
>
> My idea is to have  ignored variables in some kind of loop and reduce those variables from my varlist one by one. Once the forloop is done, create a new varlist containing only the variables I am interestd and rank them.
>
>
>
>
> Let me know if I can make it any clearer. Thank you very much and I am really sorry to be knocking at you multiple times...
>
>
> Tashi
>
>
>
>
>
>> Date: Sun, 1 Apr 2012 11:05:40 +0100
>> Subject: Re: st: passing indefinite no of arguments
>> From: [email protected]
>> To: [email protected]
>>
>> Another way to do it (no program needed)
>>
>> ds var2 var3, not
>> egen rest_total = rowtotal(`r(varlist)')
>>
>>
>> On Sun, Apr 1, 2012 at 8:57 AM, Nick Cox <[email protected]> wrote:
>> > Sorry for previous premature post.
>> >
>> > That in turn is easily broken once "the rest" is no longer
>> > homogeneous. Still, this is a sketch of what you seem to aiming for.
>> > Your idea of
>> > a do-file to do this is incomplete as a name for the new variable is
>> > needed. Warning: code not tested.
>> >
>> > program sumrest
>> > version 8.2
>> > syntax varlist(numeric) [if] [in] , Generate(string)
>> > marksample touse, novarlist
>> > qui count if `touse'
>> > if r(N) == 0 error 2000
>> > confirm new var `generate'
>> >
>> > unab all : _all
>> > local varlist : all - varlist
>> > egen `generate' = rowtotal(`varlist') if `touse'
>> > end
>> >
>> > sumrest var2 var3, generate(not2or3)
>> >
>> > Nick
>> >
>> >> On Sat, Mar 31, 2012 at 8:26 PM, tashi lama <[email protected]> wrote:
>> >>> Hello Nick,
>> >>>
>> >>> Sorry for not being clear enough. The problem with this code is you have to edit the code everytime you want to change the variables to be ignored. That is exactly why I wanted to pass those variables as arguments. So, again I wanted to be able to say
>> >>>
>> >>>
>> >>>
>> >>> do filename var2 var3 and expect to get the sum of the rest.
>> >>>
>> >>>
>> >>>
>> >>> What I am really trying to do is understand how to do this and use it for the actual program that I am working. I didn't write the actual program because it is too long and I am afraid it might bore you guys. I would be more than happy to put the actual program and actual problem I am facing if you are interested. But, I am going to be using similar concept somewhere in my actual program.
>> >>>
>> >>>
>> >>>
>> >>> Thank you very much for your time,
>> >>>
>> >>> Tashi
>> >>>
>> >>>
>> >>>
>> >>>> Date: Sat, 31 Mar 2012 17:47:55 +0100
>> >>>> Subject: Re: st: passing indefinite no of arguments
>> >>>> From: [email protected]
>> >>>> To: [email protected]
>> >>>>
>> >>>> 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...
>> >>>> >
>>

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