Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why the local value in a loop changes after use another data-set ?

    I have two datasets. The first one contains the following data. The data name is Forloop.dta.
    Code:
    clear
    input double(lpermno fyear) long apdedate float apdedateF90 str11 Station
    10001 2010 18627 18537 "7252454853" 
    10001 2011 18992 18902 "7252454853" 
    10001 2012 19358 19268 "7252454853" 
    10001 2013 19723 19633 "7252454853" 
    10001 2014 20088 19998 "7252454853" 
    10001 2015 20453 20363 "7252454853" 
    10001 2016 20819 20729 "7252454853" 
    10002 2003 16070 15980 "99819699999"
    10002 2004 16436 16346 "99819699999"
    10002 2005 16801 16711 "99819699999"
    10002 2006 17166 17076 "99819699999"
    10002 2007 17531 17441 "99819699999"
    10002 2008 17897 17807 "99819699999"
    10002 2009 18262 18172 "99819699999"
    10012 2003 16130 16040 "72297793184"
    end
    format %d apdedate
    format %td apdedateF90
    The second dataset contains the following data. The dataset name is NOAP.dta .


    Code:
    clear
    input str11 Station float(temp Date)
    "72297793184" 56.7 17965
    "72297793184" 67.9 21474
    "72297793184" 59.1 16530
    "72297793184" 63.6 17686
    "72297793184" 59.7 18344
    "72297793184" 62.6 16368
    "72297793184" 57.7 18366
    "72297793184" 58.8 17999
    "72297793184" 61.4 19115
    "72297793184" 61.4 21277
    "72297793184" 71.1 18826
    "72297793184" 61.1 19661
    "72297793184" 69.4 21368
    "72297793184" 62.4 20198
    "72297793184" 65.9 16940
    "72297793184" 63.9 19851
    "72297793184" 66.4 14466
    "72297793184" 60.6 17678
    "72297793184" 64.8 17465
    "72297793184" 60.3 14599
    end
    format %td Date


    My code is :

    Code:
    clear
    cd "C:\Data"
    use "Forloop.dta"
    local obs = _N
    forvalue Num = 1(1)`obs'{
    display `Num'
    global StartDate=apdedateF90[`Num']
    display $StartDate
    global EndDate=apdedate[`Num']
    display $EndDate
    global StationName="Station[`Num']"
    display $StationName
    use "NOAP.dta"
    display $StationName
    keep if Station==$StationName
    keep if inrange(Date,$StartDate,$EndDate)
    cd "C:\TempDta"
    save `Num',replace
    cd "C:\Data"
    use "Forloop.dta",clear
    }
    The example of the output is the following :
    Code:
    7252454853 *Before the use "NOAP.dta"
    01001099999  *After the use "NOAP.dta"
    My question is:
    Why the value of the StationName is different before and after the use "NOAP.dta" ?
    How to keep its value constant after use "NOAP.dta " ?
    Thanks for your help.

  • #2
    Originally posted by Eric Li View Post
    Why the value of the StationName is different before and after the[B] use "NOAP.[I]dta" ?
    Putting the argument in quotation marks appears to assign the formula Station[`Num'] to the global macro instead of the value of the Numth station name in the dataset. So, when you display it each time, it evaluates the formula at that moment of display. The Numth value for station name differs between datasets; you get what the formula sees at the moment of evaluation.

    How to keep its value constant after use "NOAP.dta " ?
    Change the line to
    Code:
    global StationName = Station[`Num']
    .ÿ
    .ÿversionÿ15.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿinputÿdouble(lpermnoÿfyear)ÿlongÿapdedateÿfloatÿapdedateF90ÿstr11ÿStation

    .ÿformatÿ%tdCY-N-DÿapdedateÿapdedateF90

    .ÿtempfileÿForloop

    .ÿquietlyÿsaveÿ`Forloop'

    .ÿ
    .ÿdropÿ_all

    .ÿquietlyÿinputÿstr11ÿStationÿfloat(tempÿDate)

    .ÿformatÿ%tdCY-N-DÿDate

    .ÿtempfileÿNOAP

    .ÿquietlyÿsaveÿ`NOAP'

    .ÿ
    .ÿ/*ÿclear
    >ÿcdÿ"C:\Data"
    >ÿuseÿ"Forloop.dta"ÿ*/
    .ÿuseÿ`Forloop'

    .ÿlocalÿobsÿ=ÿ_N

    .ÿforvaluesÿNumÿ=ÿ1(1)`obs'{
    ÿÿ2.ÿÿÿÿÿÿÿÿÿdisplayÿ`Num'
    ÿÿ3.ÿ
    .ÿÿÿÿÿÿÿÿÿ/*ÿglobalÿStartDate=apdedateF90[`Num']
    >ÿÿÿÿÿÿÿÿÿdisplayÿ$StartDate
    >ÿ
    >ÿÿÿÿÿÿÿÿÿglobalÿEndDate=apdedate[`Num']
    >ÿÿÿÿÿÿÿÿÿdisplayÿ$EndDateÿ*/
    .ÿ
    .ÿÿÿÿÿÿÿÿÿglobalÿStationNameÿ=ÿStation[`Num']
    ÿÿ4.ÿÿÿÿÿÿÿÿÿdisplayÿ"$StationName"
    ÿÿ5.ÿ
    .ÿÿÿÿÿÿÿÿÿ/*ÿuseÿ"NOAP.dta"ÿ*/
    .ÿÿÿÿÿÿÿÿÿuseÿ`NOAP'
    ÿÿ6.ÿÿÿÿÿÿÿÿÿdisplayÿ"$StationName"
    ÿÿ7.ÿÿÿÿÿÿÿÿÿcontinue,ÿbreak
    ÿÿ8.ÿ
    .ÿÿÿÿÿÿÿÿÿ/*ÿkeepÿifÿStation==$StationName
    >ÿÿÿÿÿÿÿÿÿkeepÿifÿinrange(Date,$StartDate,$EndDate)
    >ÿÿÿÿÿÿÿÿÿcdÿ"C:\TempDta"
    >ÿÿÿÿÿÿÿÿÿsaveÿ`Num',replace
    >ÿÿÿÿÿÿÿÿÿcdÿ"C:\Data"
    >ÿÿÿÿÿÿÿÿÿuseÿ"Forloop.dta",clearÿ*/
    .ÿ}
    1
    7252454853
    7252454853

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Thanks Joseph. It really helps a lot.

      Comment

      Working...
      X