Announcement

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

  • Loop Problem as it does not move to the second sample but works fine for whatever is the first sample

    Hello Everyone,

    I am trying to run this program on the mentioned dataset but it only works for the first sample, in my sample related loop which means if i put "under60" first it works for it and if i put over65 first it works but not for two other samples. I wonder why it is happening as I have tried my best but unable to figure out the reason and I want to run this program in one go for all the samples. You may find some of the commands in this program redundant but it does not matter since it does not affect as it works for the first specified sample. However program is very precise and I will be waiting for your input on this. Thanks


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double hhidpn float(riwy oop) double hittot float(wave freq Hospitalization under60 age60to64 over65 working hgovt spouse ritearnsemp sitearnsemp Austria Belgium CzechRepublic Switzerland)
    1100032701 2004         0  28222.52614044083 1 1 54 1 0 0 100         0 100 14619.177  13574.95 1 0 0 0
    1100032701 2007  684.8905  22797.06865355575 2 1 54 1 0 0   0  11740.98 100         0 10762.564 1 0 0 0
    1100032702 2007  45.00709  22797.06865355575 2 1  . 0 0 0 100  11740.98 100 10762.564         0 1 0 0 0
    1100032702 2004         0 28246.117630832287 1 1  . 0 0 0 100         0 100  13574.95 14619.177 1 0 0 0
    1100067401 2011         .  8189.252254102425 4 1  . 0 0 0 100         0   0  8181.979         0 1 0 0 0
    1100067401 2013  690.7431 13383.147826572063 5 1  . 0 0 0   0 13296.805   0         0         0 1 0 0 0
    1100067401 2015 101.79508 14244.525461033409 6 1  . 0 0 0   0  14142.73   0         0         0 1 0 0 0
    1100121501 2015  718.5037 14251.311799985782 6 1 74 0 0 1   0 14251.312   0         0         0 1 0 0 0
    1100121501 2011         . 13098.439884225993 4 1 74 0 0 1   0 13091.167   0         0         0 1 0 0 0
    1100121501 2013 1688.0035 13296.804937368373 5 1 74 0 0 1   0 13296.805   0         0         0 1 0 0 0
    1100149201 2011         . 38182.571359359266 4 1 63 0 1 0   0         0 100         0  38182.57 1 0 0 0
    1100149201 2013         0   47488.5890620299 5 1 63 0 1 0   0         0 100         0  47488.59 1 0 0 0
    1100149201 2015  67.86339 28502.623599971565 6 1 63 0 1 0   0 23752.186 100         0 4750.4375 1 0 0 0
    1100149202 2015         0 28502.623599971565 6 1  . 0 0 0   0 23752.186 100 4750.4375         0 1 0 0 0
    1100149202 2011         .  38182.57045651663 4 1  . 0 0 0 100         0 100  38182.57         0 1 0 0 0
    1100149202 2013  5180.573   47488.5890620299 5 1  . 0 0 0 100         0 100  47488.59         0 1 0 0 0
    1100181601 2004  104.4227 10442.269253841407 1 1  . 0 0 0   0  6265.361 100 2297.2993 1879.6085 1 0 0 0
    1100181602 2004         0 10442.269253841407 1 1  . 0 0 0 100  6265.361 100 1879.6085 2297.2993 1 0 0 0
    1100181602 2007  48.92075                  . 2 1  . 0 0 0 100         0 100         0         0 1 0 0 0
    1100188101 2011         . 18727.641700101016 4 1 85 0 0 1   0 18673.096 100         0         0 1 0 0 0
    1100188101 2013         0  19107.68138077676 5 1 85 0 0 1   0 19038.607 100         0         0 1 0 0 0
    1100188101 2015         0  9933.503641537709 6 1 85 0 0 1   0  9908.055   0         0         0 1 0 0 0
    1100188102 2011         .  18673.09517087742 4 1 90 0 0 1   0 18673.096 100         0         0 1 0 0 0
    1100188102 2013 259.02866  19107.68138077676 5 1 90 0 0 1   0 19038.607 100         0         0 1 0 0 0
    1100188102 2015  254.4877 16244.798867245698 6 1 90 0 0 1   0 16202.384 100         0         0 1 0 0 0
    1100193701 2011         .  4363.722337887615 4 1 51 0 0 0   0  4363.722   0         0         0 1 0 0 0
    1100213201 2007   782.732 12915.077520469353 2 1  . 0 0 0   0  6457.539   0         0         0 1 0 0 0
    1100213201 2004         0  11188.31493035444 1 1  . 0 0 0   0 10024.578   0  1156.103         0 1 0 0 0
    1100213601 2011         . 43201.230444387395 4 1 63 0 1 0   0 15927.587 100 27273.264         0 1 0 0 0
    1100213601 2015  814.3607  50426.23315933544 6 1 63 0 1 0   0  35628.28 100         0         0 1 0 0 0
    1100213601 2013  604.4002  59321.15772522945 5 1 63 0 1 0   0  32896.64 100 12953.503         0 1 0 0 0
    1100213603 2013  43.17144  59321.15772522945 5 1 58 0 0 0   0  32896.64 100         0 12953.503 1 0 0 0
    1100213603 2011         .  43200.85114508739 4 1 58 0 0 0   0 15927.587 100         0 27273.264 1 0 0 0
    1100213603 2015 118.76093  50426.23315933544 6 1 58 0 0 0   0  35628.28 100         0         0 1 0 0 0
    1100218002 2011         .  22909.54227390998 4 1  . 0 0 0   0 22909.543 100         0         0 1 0 0 0
    1100218002 2013  43.17144 24176.008977033405 5 1  . 0 0 0   0  24176.01 100         0         0 1 0 0 0
    1100218003 2011         . 22909.905917443557 4 1 72 0 0 1   0 22909.543 100         0         0 1 0 0 0
    1100218003 2013         0 24176.008977033405 5 1 72 0 0 1   0  24176.01 100         0         0 1 0 0 0
    1100235501 2004         0  22455.63591735869 1 1 71 0 0 0   0 22429.994 100         0         0 1 0 0 0
    1100235502 2004         0  22455.63591735869 1 1 71 0 0 0   0 22429.994 100         0         0 1 0 0 0
    1100252501 2011         . 28573.290224959946 4 1 68 0 0 0   0  28573.29 100         0         0 1 0 0 0
    1100252501 2013         0 28734.913526988275 5 1 68 0 0 0   0  28648.57 100         0         0 1 0 0 0
    1100252501 2015 262.97064 27569.501994020113 6 1 68 0 0 0   0 27315.014 100         0         0 1 0 0 0
    1100252502 2011         .  28718.74763622287 4 1 68 0 0 0   0  28573.29 100         0         0 1 0 0 0
    1100252502 2013   388.543 28734.913526988275 5 1 68 0 0 0   0  28648.57 100         0         0 1 0 0 0
    1100252502 2015  458.0779 27569.501994020113 6 1 68 0 0 0   0 27315.014 100         0         0 1 0 0 0
    1100257301 2011         .  25988.87567017261 4 1  . 0 0 0   0  25982.33 100         0         0 1 0 0 0
    1100257301 2013 172.68578  25255.29509207954 5 1  . 0 0 0   0 25255.295 100         0         0 1 0 0 0
    1100257302 2013 172.68578  25255.29509207954 5 1 69 0 0 0   0 25255.295 100         0         0 1 0 0 0
    1100257302 2011         . 25982.330086839174 4 1 69 0 0 0   0  25982.33 100         0         0 1 0 0 0
    1100280001 2011         .   23.1209056356435 4 1  . 0 0 0 100         0   0         0         0 1 0 0 0
    1100280001 2013 25.902866 18247.256967272468 5 1  . 0 0 0   0         0   0 18132.006         0 1 0 0 0
    1100280001 2015  58.53217  53587.90078934619 6 1  . 0 0 0   0  23005.69   0 30538.525         0 1 0 0 0
    1100296502 2013         0  26.70923854446815 5 1 87 0 0 0   0         0   0         0         0 1 0 0 0
    1100296502 2011         .  7643.786961866473 4 1 87 0 0 0   0  7636.514   0         0         0 1 0 0 0
    1100296502 2015         0  7634.631321420955 6 1 87 0 0 0   0  7634.631   0         0         0 1 0 0 0
    1100319401 2011         . 19654.932696902135 4 1  . 0 0 0   0  19618.57 100         0         0 1 0 0 0
    1100319401 2013         0                  . 5 1  . 0 0 0   0 18252.887 100         0         0 1 0 0 0
    1100319401 2015  25.44877                  . 6 1  . 0 0 0   0 18187.389 100         0         0 1 0 0 0
    1100319402 2011         . 19618.568344086405 4 1  . 0 0 0   0  19618.57 100         0         0 1 0 0 0
    1100321301 2011         . 12000.781894504853 4 1 69 0 0 0   0 12000.236   0         0         0 1 0 0 0
    1100321301 2013         0 12088.004488516703 5 1 69 0 0 0   0 12088.005   0         0         0 1 0 0 0
    1100321301 2015  76.34631  12351.13689332101 6 1 69 0 0 0   0 12351.137   0         0         0 1 0 0 0
    1100368301 2011         . 39396.048910150945 4 1  . 0 0 0 100         0 100 18436.727  20954.96 1 0 0 0
    1100368302 2011         .  39391.68518763966 4 1  . 0 0 0 100         0 100  20954.96 18436.727 1 0 0 0
    1100423401 2011         .  26218.69838014142 4 1 53 0 0 0   0 13491.175 100         0 12727.523 1 0 0 0
    1100423401 2004  2401.722                  . 1 1 53 0 0 0   0 12605.907 100         0         0 1 0 0 0
    1100423401 2007   235.798 23100.377292294048 2 1 53 0 0 0   0 12328.028 100         0 10762.564 1 0 0 0
    1100423402 2011         .  26291.42708577288 4 1  . 0 0 0 100 13491.175 100 12727.523         0 1 0 0 0
    1100423402 2007   97.8415 23100.377292294048 2 1  . 0 0 0 100 12328.028 100 10762.564         0 1 0 0 0
    1100437901 2004 156.63403  10024.57848368775 1 1 76 0 0 1   0 10024.578   0         0         0 1 0 0 0
    1100437901 2007         0   6443.84095074327 2 1 76 0 0 1   0  6434.057   0         0         0 1 0 0 0
    1100437901    .         .                  . 3 1 76 0 0 1   0         .   0         .         . 1 0 0 0
    1100437901 2011         .  8181.979383539278 4 1 76 0 0 1   0  8181.979   0         0         0 1 0 0 0
    1100485501 2004  626.5361 13815.122222832182 1 1  . 0 0 0   0 13783.796 100         0         0 1 0 0 0
    1100485501 2007         0 29352.448910157622 2 1  . 0 0 0   0  28961.08 100         0         0 1 0 0 0
    1100485502 2004 187.96085 13783.795415070657 1 1 78 0 0 1   0 13783.796 100         0         0 1 0 0 0
    1100485502 2007 146.76224 29352.448910157622 2 1 78 0 0 1   0  28961.08 100         0         0 1 0 0 0
    1100485502    .         .                  . 3 1 78 0 0 1   0         .   0         .         . 1 0 0 0
    1100485502 2011         .  8182.856558813008 4 1 78 0 0 1   0  8181.979   0         0         0 1 0 0 0
    1100485502 2013         0  9116.898542004037 5 1 78 0 0 1   0  9107.448   0         0         0 1 0 0 0
    1100485502 2015         0 10570.917046599205 6 1 78 0 0 1   0  10566.33   0         0         0 1 0 0 0
    1100497401 2007 127.19395 15312.194181465558 2 1  . 0 0 0   0 15263.273   0         0         0 1 0 0 0
    1100497401 2004  26.10567  36902.75519744703 1 1  . 0 0 0   0 36892.313   0         0         0 1 0 0 0
    1100546001    .         .                  . 3 1 67 0 0 1   0         .   0         .         . 1 0 0 0
    1100546001 2007  176.1147                  . 2 1 67 0 0 1   0 17807.152 100 12551.504         0 1 0 0 0
    1100546001 2004  73.09589                  . 1 1 67 0 0 1   0 20024.096 100 1253.0723         0 1 0 0 0
    1100596001 2011         .  9.091088203932532 4 1  . 0 0 0   0         0   0         0         0 1 0 0 0
    1100678802 2013         0 27239.493051228976 5 1  . 0 0 0 100         0 100  21758.41  5437.914 1 0 0 0
    1100678802 2015  72.10485  35159.80092664269 6 1  . 0 0 0 100         0 100 26127.404  8650.664 1 0 0 0
    1100678804 2011         .                  . 4 1  . 0 0 0 100 1090.9305 100  5000.099         0 1 0 0 0
    1100678804 2013         0 27239.493051228976 5 1  . 0 0 0 100         0 100  5437.914  21758.41 1 0 0 0
    1100678804 2015         0  35159.80092664269 6 1  . 0 0 0 100         0 100  8650.664 26127.404 1 0 0 0
    1100714401 2015  169.6585  17353.36736194026 6 1  . 0 0 0   0 16813.154   0         0         0 1 0 0 0
    1100714401 2011         .  19927.66534302011 4 1  . 0 0 0   0  19909.48   0         0         0 1 0 0 0
    1100714401 2013 103.61147 17009.549173127074 5 1  . 0 0 0   0   7770.86   0         0         0 1 0 0 0
    1100717001 2013 215.85722 34278.127013865225 5 1  . 0 0 0   0 33846.414 100         0         0 1 0 0 0
    1100717001 2011         . 27273.264611797596 4 1  . 0 0 0   0 27273.264 100         0         0 1 0 0 0
    1100717002 2011         .  27273.26824823305 4 1  . 0 0 0   0 27273.264 100         0         0 1 0 0 0
    1100717002 2013         0 34278.127013865225 5 1  . 0 0 0   0 33846.414 100         0         0 1 0 0 0
    end
    Here is my code please save data and specify the directory in order to run it because I am using country wise dummies so condition I put with dta file (line 20) is necessary.

    Code:
    * SUMMARY STATISTICS FOR ALL SAMPLES
    local samples "under60 age60to64 over65"
    local spouses "freq"
    local specs "freq"
    local country "Austria Belgium CzechRepublic Denmark"
    local outcomes "oop Hospitalization working ritearnsemp sitearnsemp hgovt hittot"
    
    
    
    
    foreach samp in `samples' {
    foreach spouse in `spouses' {
    foreach spec in `specs' {
    foreach country in `country' {
    
    
    
    di "Sample = `samp' , country = `country'"
    *use "D:\New folder\Dropbox\Cross-country health risk\AER materials\Replication Kit\HRS\Data\HRS_long.dta" if `samp'==1 & `spec'==1 & `spouse'==1, clear
    use "Put the data file here.dta" if `samp'==1 & `spec'==1 & `spouse'==1 & `country'==1, clear
     
     
     
    format riwy %td
    gen year_analyze = riwy - 1
     
    
    
    
    * Determine number of variables to set columns of output matrix
    local numvar = 0
    foreach i of varlist `outcomes' {
        local numvar = `numvar'+1
    }
    matrix results = J(2, `numvar', .)
    
    * Set column names
    ds `outcomes'
    local vars `r(varlist)'
    matrix colnames results = `vars'
    
    * Loop over the list of outcomes
    local j = 1
    foreach var of varlist `outcomes' {
    
        di "Running Variable: `var'"
        count if `var' < .
        local N = r(N)
        count if `var' == 1
        local N1 = r(N)
        count if `var' == 0
        local N0 = r(N)
        local dummy = (`N0' + `N1' == `N')
      
        * Compute unconditional mean, median, etc.
        summ `var' [aweight=rwtresp], det
        if inlist("`var'","year_analyze")    {
            local mean_`j'_`samp'_`country' = string(r(mean),"%10.0f")
            }
        else  {
            local mean_`j'_`samp'_`country' = string(r(mean),"%10.2f")
            }
        local sd_`j'_`samp'_`country' = string(r(sd),"%10.2f")
        
        local j = `j'+1
        }
        
        
    }
    }
    }
    }
    Note: You can change few dummies for the other 3 countries so it picks up the sample. At this moment i don't know how can i add more data with the help of dataex so only austria is 1 right now.
    Last edited by Syed Raza; 09 Jul 2018, 10:25.

  • #2
    Consider the following simplification of your code.
    Code:
    . local samples "under60 age60to64 over65"
    
    . local country "Austria Belgium CzechRepublic Denmark"
    
    . foreach samp in `samples' {
      2. display "in samp loop - samp:  `samp'  country: `country'
      3. foreach country in `country' {
      4. display "in country loop - samp:  `samp'  country: `country'
      5. }
      6. }
    in samp loop - samp:  under60  country: Austria Belgium CzechRepublic Denmark
    in country loop - samp:  under60  country: Austria
    in country loop - samp:  under60  country: Belgium
    in country loop - samp:  under60  country: CzechRepublic
    in country loop - samp:  under60  country: Denmark
    in samp loop - samp:  age60to64  country: 
    in samp loop - samp:  over65  country:
    Your problem is you have used the local macro country for both the list of countries, and for the individual country in the loop.

    Comment


    • #3
      William Lisowski When i replace this code in above it gives me an error "invalid command" for "in." I have replaced above-mentioned loops in my code for country and sample by your code and after closing the loops i am using this "in samp" thing. I am not certain which is the right way to do it. Can you please tell me where exactly i need to put your code in place of my code.

      Comment


      • #4
        In addition to this problem is not with the country related loop it is with the sample loop. It does not move forward to next sample after executing the operation for first sample for all the countries included.

        Comment


        • #5
          this problem is not with the country related loop it is with the sample loop.
          This is not correct, the problem is with the country loop

          The first time it is run it destroys the list of countries in the local macro country by reusing that local macro as the foreach variable.

          You apparently did not put sufficient effort into seeing what post #2 is telling you, if you copied the code block into your program. The code block was copied from Stata's Results window, and the lines beginning with "in" are the output of running the loops.

          The code presented is not a solution to your problem, it is a simplified demonstration of your problem. Notice that it fails in exactly the same way your code fails: it only goes through the country loop for the first sample. And looking at the output, we can see that it does move to the second and third samples, but the country list is empty and so it is not runl

          Your problem is you have used the local macro country for both the list of countries, and for the individual country in the loop.
          That explains exactly what is wrong with your code, which you apparently have not understood, since you convinced yourself the problem was with the sample loop. If you present a problem to others for advice, you need to be open to the idea that your understanding of the source of the problem is incorrect. The problem is indeed with the country loop - it is not being run because the list of countries has been replaced with an empty list.

          In your code, you have the following two lines
          Code:
          local country "Austria Belgium CzechRepublic Denmark"
          and
          Code:
          foreach country in `country' {
          They should be
          Code:
          local countries "Austria Belgium CzechRepublic Denmark"
          and
          Code:
          foreach country in `countries' {
          where I highlight in red the two changes I made to your code.

          Comment


          • #6
            thank you so much problem solved.
            Last edited by Syed Raza; 13 Jul 2018, 01:06.

            Comment

            Working...
            X