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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Some basic help on programming


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Some basic help on programming
Date   Mon, 16 Apr 2012 19:02:12 +0100

Sorry, but that doesn't make it easier for me to comment helpfully. My
guess is that your code from the outset won't actually do anything
like what you want to do, even if it is legal as far as Stata is
concerned.

As I said, your code opens with a loop over values 3/9 but you use the
name -level- to refer to the possibilities. But

1. That will only work if -level- is a name of an existing variable or
scalar, or the abbreviation thereof. But if -level- is a variable,
such code is almost always not going to do to what the author intends.
See

http://www.stata.com/support/faqs/lang/ifqualifier.html

for an explanation showing that (e.g.)

if level==3 | level==5 {

will mean

if level[1] == 3 | level[1] == 5 {

if -level- is a variable. Also if -level- is a scalar, then where is
it defined?

2. You don't address my earlier point that you are looping with a
macro named -l- but use -level- to refer to the values 3/9. That
doesn't seem to hang together.

Nick

On Mon, Apr 16, 2012 at 6:04 PM, Prakash Singh <prakashbhu@gmail.com> wrote:
> Nick, I am able to run the code till - save "level`l'data.dta",replace
> - successfully but for remaining for remaining part of the code it
> either says invalid syntax or data not found.
>
> Please have look on code lines after - save "level`l'data.dta",replace.
>
>
> Prakash
>
>
> On Mon, Apr 16, 2012 at 8:12 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>> Posting a chunk of code and giving no details of precisely what errors
>> you are getting is not strongly recommended. But a wild guess is this:
>>
>> Your branches like
>>
>> if level==3 | level==5 {
>> }
>> if level==4| level ==6| level==7 {
>> }
>> if level==8 {
>> }
>> if level==9 {
>>  }
>>
>> make reference to -level- but what do you think it is? From the outer
>> loop I suspect that
>>
>> level
>>
>> should in each case be
>>
>> `l'
>>
>> Nick
>>
>> On Mon, Apr 16, 2012 at 1:23 PM, Prakash Singh <prakashbhu@gmail.com> wrote:
>>> Friedrich,
>>> I know that there is some thing worng with my code. when I am merging
>>> the level data ie last part of the code but what ever changes I have
>>> tried some or the other error is coming.
>>>
>>> Here is the code
>>>
>>> local levels "3 4 5 6 7 8 9"
>>> foreach l of local levels {
>>>  use "level`l'_list.dta", clear
>>>  if level==3 | level==5 {
>>>  local vars "item_code asicc_code unit_quantity quantity value"
>>>  }
>>>  if level==4| level ==6| level==7 {
>>>  local vars "item_code value"
>>>  }
>>>  if level==8 {
>>>  local vars "item_code value_ownd_assets value_hired_assets
>>> addition_ownd_assets sign rent_payable"
>>>  }
>>>  if level==9 {
>>>  local vars "item_code loan_outs int_payable"
>>>  }
>>>  levelsof item_code, local(items)
>>>  foreach i of local items {
>>>   preserve
>>>   keep if item_code==`i'
>>>   foreach var of varlist `vars' {
>>>     ren `var' `var'_`i'
>>>   }
>>>   sort common_id
>>>   save "item`i'.dta", replace
>>>   restore
>>>  }
>>>  contract common_id
>>>  drop _freq
>>>  foreach i of local items {
>>>   merge common_id using "item`i'.dta"
>>>   drop _merge
>>>   sort common_id
>>>  }
>>> save "level`l'data.dta",replace
>>> }
>>> contract common_id
>>>  drop _freq
>>>  foreach l of local levels {
>>>   merge common_id using "level`l'data.dta"
>>>   drop _merge
>>>   sort common_id
>>>  }
>>> save "level3_9data.dta",replace
>>>

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