Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: passing indefinite no of arguments


From   tashi lama <ltashi32@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: passing indefinite no of arguments
Date   Mon, 2 Apr 2012 19:20:19 +0000

Hello Nick, 

 

    I tried running the code with syntax as a do file. There was an error which reads option generate() not allowed. Any idea?

 

Tashi

----------------------------------------
&#62; Date&#58; Mon, 2 Apr 2012 18&#58;57&#58;19 &#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; Stata has its own terminology. A program, strict sense, is defined by&#13;&#10;&#62; a -program- command. What you have below is a do-file or part of it.&#13;&#10;&#62; That&#39;s fine. Many Stata users write do-files all the time, and only&#13;&#10;&#62; do-files, and that&#39;s fine.&#13;&#10;&#62;&#13;&#10;&#62; -syntax-, -marksample- and -touse- are all basic Stata programming&#13;&#10;&#62; commands, which in practice are used mostly with programs as defined&#13;&#10;&#62; strictly. They are well documented. Nothing obliges you to write your&#13;&#10;&#62; own programs in Stata&#39;s sense. I showed you how one fairly experienced&#13;&#10;&#62; user, namely me, would approach your last problem, but !
 there is&#13;&#10;&#62; nothing compulsory about what I did.&#13;&#10;&#62;&#13;&#10;&#62; However, I don&#39;t see anything new in this posting. Techniques for&#13;&#10;&#62; omitting variables from lists of variables have been covered&#13;&#10;&#62; repeatedly in the threads that you have started. References have been&#13;&#10;&#62; given to programs already written to do part of what you want. You&#13;&#10;&#62; seem determined to write your own code. I respect that but I don&#39;t&#13;&#10;&#62; have anything to add to what I have tried to explain in several posts.&#13;&#10;&#62;&#13;&#10;&#62; Nick&#13;&#10;&#62;&#13;&#10;&#62; On Mon, Apr 2, 2012 at 5&#58;14 PM, tashi lama &#60;ltashi32&#64;hotmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Hello Nick,&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Thank you. I tried reading/running your code that uses syntax. I will be frank with you, I couldn&#39;t follow your code even after spending quite some time t!
 rying 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.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; date var1 var2 var3 var4 var5 var6&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; 01jan2010 1 3 5 2 1 6&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; 02jan2010 6 2 1 5 6 7&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; 03jan2010 4 2 5 7 8 1&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; This was my program to rank and it works...&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; clear&#13;&#10;&#62; &#62; args firm_id start_date end_date graph_type&#13;&#10;&#62; &#62; if &#34;&#96;graph_type&#39;&#34;&#61;&#61;&#34;rank&#34; &#123;&#13;&#10;&#62; &#62; collapse &#40;sum&#41; var&#42; if tin&#40;&#96;start_date&#39;,&#96;end_date&#39;&#41;&#13;&#10;&#62; &#62; local counter 1&#13;&#10;&#62; &#62; foreach !
 x of varlist var&#42; &#123;&#13;&#10;&#62; &#62; if &#96;x&#39;&#33;&#61;&#96;firm_id&#39; &#38; &#96;x&#39;&#62;&#96;firm_id&#39; &#123;&#13;&#10;&#62; &#62; local &#43;&#43;counter&#13;&#10;&#62; &#62; &#125;&#13;&#10;&#62; &#62; &#125;&#13;&#10;&#62; &#62; list&#13;&#10;&#62; &#62; di &#34; I rank &#96;counter&#39; &#34;&#13;&#10;&#62; &#62; exit&#13;&#10;&#62; &#62; &#125;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; 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&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; do filename var4 01jan2010 03jan2010 rank var5 &#40;Note&#58; I want to have liberty to enter any no of variables to be ignored in my arguments....&#41;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; My idea is to have ignored variables in some ki!
 nd 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.&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; 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...&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62; Tashi&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#13;&#10;&#62; &#62;&#62; Date&#58; Sun, 1 Apr 2012 11&#58;05&#58;40 &#43;0100&#13;&#10;&#62; &#62;&#62; Subject&#58; Re&#58; st&#58; passing indefinite no of arguments&#13;&#10;&#62; &#62;&#62; From&#58; njcoxstata&#64;gmail.com&#13;&#10;&#62; &#62;&#62; To&#58; statalist&#64;hsphsun2.harvard.edu&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; Another way to do it &#40;no program needed&#41;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; ds var2 var3, not&#13;&#10;&#62!
 ; &#62;&#62; egen rest_total &#61; rowtotal&#40;&#96;r&#40;varlist&#41;&#39;&#41;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62;&#13;&#10;&#62; &#62;&#62; On Sun, Apr 1, 2012 at 8&#58;57 AM, Nick Cox &#60;njcoxstata&#64;gmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#62; &#62; Sorry for previous premature post.&#13;&#10;&#62; &#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62; That in turn is easily broken once &#34;the rest&#34; is no longer&#13;&#10;&#62; &#62;&#62; &#62; homogeneous. Still, this is a sketch of what you seem to aiming for.&#13;&#10;&#62; &#62;&#62; &#62; Your idea of&#13;&#10;&#62; &#62;&#62; &#62; a do-file to do this is incomplete as a name for the new variable is&#13;&#10;&#62; &#62;&#62; &#62; needed. Warning&#58; code not tested.&#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; , Genera!
 te&#40;string&#41;&#13;&#10;&#62; &#62;&#62; &#62; marksample touse, n

ovarlist&#13;&#10;&#62; &#62;&#62; &#62; qui count if &#96;touse&#39;&#13;&#10;&#62; &#62;&#62; &#62; if r&#40;N&#41; &#61;&#61; 0 error 2000&#13;&#10;&#62; &#62;&#62; &#62; confirm new var &#96;generate&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62; unab all &#58; _all&#13;&#10;&#62; &#62;&#62; &#62; local varlist &#58; all - varlist&#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; sumrest var2 var3, generate&#40;not2or3&#41;&#13;&#10;&#62; &#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62; Nick&#13;&#10;&#62; &#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62; On Sat, Mar 31, 2012 at 8&#58;26 PM, tashi lama &#60;ltashi32&#64;hotmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62; Hello Nick,&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62!
 ; 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&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62; do filename var2 var3 and expect to get the sum of the rest.&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62; 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&#39;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 t!
 o be using similar concept somewhere in my actual program.&#13;&#10;&#

62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62; Thank you very much for your time,&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62; Tashi&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; Date&#58; Sat, 31 Mar 2012 17&#58;47&#58;55 &#43;0100&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; Subject&#58; Re&#58; st&#58; passing indefinite no of arguments&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; From&#58; njcoxstata&#64;gmail.com&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; To&#58; statalist&#64;hsphsun2.harvard.edu&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; Frankly, this is very confused. You need to keep separate macro&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#6!
 2;&#62; manipulations and variable calculations.&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; Also, thisn&#39;t really a problem that needs a program or a do-file at all.&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; local myvars &#58; var2 var3&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; unab all &#58; var&#42;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; local rest &#58; list all - myvars&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; egen rest_total &#61; rowtotal&#40;&#96;rest&#39;&#41;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; I advise trying simpler problems and studying the User&#39;s manual much&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; more slowly and carefully.&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; Nick&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13!
 ;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; On Sat, Mar 31, 2012 at 2&

#58;10 PM, tashi lama &#60;ltashi32&#64;hotmail.com&#62; wrote&#58;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; That sounds great. Here is a slightly diff problem but with a similar flavor..Any idea&#63;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; I have a dataset as follows&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; var1 var2 var3 va4 var5&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; 4 5 6 3 3&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; I would like to enter variables as arugments and find the sum of the rest. Here is my attempt&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; &#42;sum of the selected vars&#13;&#10;&#62!
 ; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; tokenize &#96;0&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; local length&#58; word count &#96;0&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; local varlist var&#42;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; forvalues i&#61;1/length &#123;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; local var_new&#61;varlist-&#96;i&#39; /&#42; i am not so sure I can do this&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; replace varlist&#61;&#96;var_new&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; drop var_new&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; &#125;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10!
 ;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; egen total&#61;rowtotal&#

40;&#96;varlist&#39;&#41;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; so if I run,&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; do filename var2 var3 , I expect to get the sum of var1 var4 and var5.&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; I am thinkin I could also do sth like this,&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; tokenize &#96;0&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; local length&#58;word count &#96;0&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; unab varlist&#58;var&#42;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; forvalues i&!
 #61;1/length &#123;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; local var_new&#58;list varlist- &#96;i&#39;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; &#125;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; egen total&#61;rowtotal&#40;&#96;var_new&#39;&#41;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62; Essentially I am trying to learn to subtract certain variables from the list of variables or a string. Any help will be highly appreciated...&#13;&#10;&#62; &#62;&#62; &#62;&#62;&#62;&#62; &#62;&#13;&#10;&#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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index