Announcement

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

  • #16
    Referring to #4, what purpose does var
    educ_total
    serve since I would be using the
    prop_nonvetedu_*
    to understand distribution of educational groups across a county with or without FQHC.
    The idea is to calculate the proportion represented by each education level. To do that you need a total to serve as the denominator, and that's what educ_total is.

    Referring to #14: For the foreach command, I am assuming that if i wanted to identify variables with different date ranges, I would repeat the foreach command and change the in range date?

    Well, evidently you can't use the exact same code if you need different age ranges for different variables. So, yes you need a separate loop for each different date range, but variables with the same date range can go together in a single loop.

    If there are a lot of different date ranges, there is a way to use local macros to dynamically select the date range and have a single loop for all variables, but it's somewhat complicated to set up, and the code tends to be opaque, so I only recommend it if there is a large number of different date ranges to contend with. If it's just, say 5 or fewer, just do that many separate loops.

    Comment


    • #17
      Clyde Schechter , following up on the commands shared in #14 and your helpful response in #16.

      After running the -ds- and -foreach- command to have observations of one variable applies across multiple years, I got the following error results

      9672 contradictions in 35530 observations
      assertion is false
      r(9);

      end of do-file
      ]

      I have quite a few variables that had to be included in the -ds- command section and quite a few definitely more than 5 that had to have the foreach command applied. I am trying to avoid the local macros approach to do this since I am not advanced in Stata but I am not sure a workaround.

      In trying to assure when i do look at some of the variables across multiple years, I create a variable for the distribution of the observations across a county like in #4 - for example the variable for number persons educated in a county 2012-16 is only population that number in the year 2012 and it is blank for 2013-2016.

      I don't think however I would need to apply this for something like census population 2010? I would not have to apply the foreach command for that to populate across all the years but rather that population estimate would be used to represent it across 2010-2018? Hope that makes sense.

      Comment


      • #18
        The error message you are getting says that for some variable that came out of the -ds- command, it is not true that only a single year has a non-missing value. This seems to correspond to what you are saying in your last two paragraphs. I wrote the code on the assumption that all variables except
        wanted ever_had_fqhc year n_county, required filling in. Evidently this is not the case.

        If the number of variables that do need filling in is smaller than the number of variables that don't need filling in, then just omit the -ds- command, and replace `r(varlist)' by the actual list of names of all the variables that need to be filled in.

        If the number of variables that don't need filling in is smaller, then keep the -ds- command and replace wanted ever_had_fqhc year n_county in the -ds- command by the complete list of variables that do not need to be filled in.

        I have quite a few variables that had to be included in the -ds- command section and quite a few definitely more than 5 that had to have the foreach command applied. I am trying to avoid the local macros approach to do this since I am not advanced in Stata but I am not sure a workaround.

        What matters here is not how many variables need to be filled in, but how many different date ranges there are. In your original example you had 4 variables needing filling in, but they all were to be filled in with the same date range, so that's only one date range for those four variables. How many different date ranges do you have?

        If you want specific help with code, please post back, using the -dataex- command to show example data again, this time including more variables needing filling in (including some from at least two different date ranges), and some variables that don't need to be filled in (in addition to wanted, ever_had_fqhc, year, and n_county). Explain which variables are to be filled in over which date ranges, and which are not to be filled in at all.

        Comment


        • #19
          Hi Clyde,

          Sorry for the late reply. I pasted below some of the variables that represented the different date ranges.I also included the variable labels to identify the year groups for each variable.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(wanted ever_had_fqhc) int fqhc byte year float n_county long vets25yo double uninsuredperct int(totalMD_active totactive_nhsc) long(povstat popwmale) double nhsc_fteprovider
          1 1 2 15 1     .  9.4  39  2      . 20925    0
          1 1 0 18 1     .    .   .  2      .     .    0
          1 1 2 17 1     .    .   .  2      .     .   .4
          1 1 2 16 1     .  8.5  39  2      . 20979    0
          1 1 0  8 1     .    .   .  .      .     .    .
          1 1 2 13 1     . 13.4   .  .      . 21106  .25
          1 1 0  6 1     .    .   .  .      .     .    .
          1 1 2 14 1     .   11   .  .      . 21134  .25
          1 1 2 12 1  5032 12.8   .  .  54598 21383    .
          1 1 1 10 1     . 13.6  36  .      .     .    .
          1 1 0  4 1     .    .   .  .      .     .    .
          1 1 2 11 1     . 13.9   .  .      . 21346    .
          1 1 4 17 2     .    .   .  7      .     . 5.95
          1 1 0 18 2     .    .   .  8      .     .    3
          1 1 3 16 2     . 10.7 405  9      . 88550 3.45
          1 1 3 15 2     . 11.5 396 10      . 86524 3.45
          1 1 0  6 2     .    .   .  .      .     .    .
          1 1 3 12 2 19266 15.8   .  . 196640 81423    .
          1 1 0  4 2     .    .   .  .      .     .    .
          1 1 3 11 2     . 16.6   .  .      . 79884    .
          1 1 3 14 2     . 16.1   .  .      . 85143  4.5
          1 1 0  8 2     .    .   .  .      .     .    .
          1 1 3 10 2     . 19.1 376  .      .     .    .
          1 1 3 13 2     . 17.4   .  .      . 83436  5.5
          1 1 3 17 3     .    .   .  3      .     .    0
          1 1 3 15 3     . 13.3  13  3      .  7099    0
          1 1 0 18 3     .    .   .  3      .     .    0
          1 1 3 16 3     . 12.5  12  3      .  6846    0
          1 1 3 13 3     . 17.5   .  .      .  7288    0
          1 1 0  8 3     .    .   .  .      .     .    .
          1 1 0  6 3     .    .   .  .      .     .    .
          1 1 3 14 3     . 15.3   .  .      .  7218    0
          1 1 3 11 3     . 18.9   .  .      .  7427    .
          1 1 3 12 3  1740 17.5   .  .  23641  7397    .
          1 1 3 10 3     . 18.5  13  .      .     .    .
          1 1 0  4 3     .    .   .  .      .     .    .
          1 1 2 17 4     .    .   .  5      .     . 6.39
          1 1 0 18 4     .    .   .  5      .     . 6.13
          1 1 2 15 4     . 11.9  18  5      .  8863    3
          1 1 2 16 4     .  9.7  19  6      .  8873    3
          1 1 0  4 4     .    .   .  .      .     .    .
          1 1 0  8 4     .    .   .  .      .     .    .
          1 1 1 14 4     . 13.6   .  .      .  8886    1
          1 1 0  6 4     .    .   .  .      .     .    .
          1 1 1 10 4     . 17.7   6  .      .     .    .
          1 1 1 12 4  1270 15.1   .  .  20603  8903    .
          1 1 1 13 4     . 15.1   .  .      .  8904    1
          1 1 1 11 4     .   16   .  .      .  8987    .
          1 1 0 18 5     .    .   .  3      .     .    1
          1 1 1 17 5     .    .   .  3      .     .    1
          1 1 1 15 5     .   14  16  3      . 27308    0
          1 1 1 16 5     . 12.1  15  4      . 27350    0
          1 1 1 10 5     . 19.3  17  .      .     .    .
          1 1 1 12 5  4460 18.3   .  .  57099 27498    .
          1 1 1 13 5     . 17.6   .  .      . 27467    0
          1 1 0  4 5     .    .   .  .      .     .    .
          1 1 1 11 5     . 18.1   .  .      . 27406    .
          1 1 1 14 5     . 16.5   .  .      . 27374    0
          1 1 0  6 5     .    .   .  .      .     .    .
          1 1 0  8 5     .    .   .  .      .     .    .
          1 1 1 15 6     . 14.5   4  0      .  1797    0
          1 1 1 17 6     .    .   .  0      .     .    0
          1 1 1 16 6     . 13.2   4  0      .  1653    0
          1 1 0 18 6     .    .   .  1      .     .    0
          1 1 1 13 6     . 18.8   .  .      .  1808    0
          1 1 0  6 6     .    .   .  .      .     .    .
          1 1 0  8 6     .    .   .  .      .     .    .
          1 1 1 14 6     . 17.3   .  .      .  1788    0
          1 1 1 11 6     . 15.7   .  .      .  1779    .
          1 1 1 10 6     . 16.2   8  .      .     .    .
          1 1 0  4 6     .    .   .  .      .     .    .
          1 1 1 12 6   406 17.5   .  .  10154  1762    .
          1 1 1 17 7     .    .   .  1      .     .    0
          1 1 1 16 7     . 10.7  12  1      .  5071    0
          1 1 0 18 7     .    .   .  1      .     .    0
          1 1 1 15 7     . 12.2  11  2      .  5157    0
          1 1 1 12 7  1332 16.6   .  .  19977  5293    .
          1 1 1 14 7     . 14.4   .  .      .  5199    0
          1 1 1 13 7     . 15.7   .  .      .  5230  .25
          1 1 1 10 7     . 17.1  16  .      .     .    .
          1 1 0  8 7     .    .   .  .      .     .    .
          1 1 0  4 7     .    .   .  .      .     .    .
          1 1 0  6 7     .    .   .  .      .     .    .
          1 1 1 11 7     . 16.4   .  .      .  5415    .
          1 1 3 17 8     .    .   .  2      .     .  1.4
          1 1 0 18 8     .    .   .  2      .     .    0
          1 1 3 15 8     . 11.4 199  2      . 42610    0
          1 1 3 16 8     . 11.2 195  2      . 42077   .4
          1 1 0  8 8     .    .   .  .      .     .    .
          1 1 2 11 8     . 16.9   .  .      . 43667    .
          1 1 3 14 8     . 14.2   .  .      . 42792    0
          1 1 2 12 8 10235 15.3   .  . 112690 43426    .
          1 1 2 13 8     .   16   .  .      . 43334    0
          1 1 0  6 8     .    .   .  .      .     .    .
          1 1 2 10 8     . 16.1 201  .      .     .    .
          1 1 0  4 8     .    .   .  .      .     .    .
          1 1 1 15 9     . 13.3  29  2      .  9574    1
          1 1 1 16 9     . 10.7  32  2      .  9490    1
          1 1 2 17 9     .    .   .  2      .     .    1
          1 1 0 18 9     .    .   .  3      .     .    1
          end
          label values n_county n_county
          label def n_county 1 "Alabama Autauga", modify
          label def n_county 2 "Alabama Baldwin", modify
          label def n_county 3 "Alabama Barbour", modify
          label def n_county 4 "Alabama Bibb", modify
          label def n_county 5 "Alabama Blount", modify
          label def n_county 6 "Alabama Bullock", modify
          label def n_county 7 "Alabama Butler", modify
          label def n_county 8 "Alabama Calhoun", modify
          label def n_county 9 "Alabama Chambers", modify
          label var wanted "1 if n_county started out with 0 FQHCs and then got 1 at some point in time and " 
          label var ever_had_fqhc "1= for all obs of any n_county that has had 1 or more FQHC's at any point in tim" 
          label var fqhc "# of Federally Qualified Health Center 2010-2017" 
          label var year "Reshaped Year variable panel data" 
          label var n_county "group(statename countyname)" 
          label var vets25yo "Veterans 25+ yrs 2012-16" 
          label var uninsuredperct "% <65 without Health Insurance 2010-2016" 
          label var totalMD_active "Total active nonfed/fed MDs 2010/15/16" 
          label var totactive_nhsc "# NHSC total active sites 2015-18" 
          label var povstat "# Pers w/Pov Status Determined 2012-16" 
          label var popwmale "Total white male pop 2011-2016" 
          label var nhsc_fteprovider "# NHSC FTE providers 2013-2018"

          Comment


          • #20
            So it appears that each variable has its own idiosyncratic range. There are as many different ranges as there are variables. That being the case, there is really no simpler solution than writing a separate command for each variable. You can check that there is only one value to be copied in a loop. But since the ranges differ, the actual copying has to be individualized.

            Code:
            foreach v of varlist fqhc vets25yo uninsuredperct totalMD_active totactive_nhsc povstat popwmale nhsc_fteprovider {
                by county (`v'), sort: assert `v' == `v'[1] | missing(`v')
            }
            
            by county (fqhc), sort: replace fqhc = fqhc[1] if inrange(year, 2010, 2017)
            by county (vets25yo), sort: replace vets25yo = vets25yo[1] if inrange(year, 2012, 2016)
            by county (uninsuredperct), sort: replace uninsuredperct = uninsuredperct[1] if inrange(year, 2010, 2016)
            // ... ETC.

            Comment


            • #21
              Thanks Clyde. So if i had several variables within the same range, would I have to individualize the code? So i have 10 variables with the 2012-2016 range and another 15 in the 2010-2016 range, and so on.

              So why would you recommend the var county rather than n_county? n_county was a variable consisting of countyname and statename a numeric variable to index the counties.

              And if I understand the following correctly:

              Code:
              by county (vets25yo), sort: replace vets25yo = vets25yo[1] if inrange(year, 2012, 2016)
              this would create populate all the data for vets25yo that exist in 2012 and apply it to all the missing spaces in 2013, 2014, 2015, 2016.

              However if i had several variables that were populated for 2012 and i wanted it to do the same thing as vets25yo would I include all the other variables in the parenthesis? so

              Code:
              by county (vetsedu_hs vetsedu_hsplus ), sort: replace vets25yo = vets25yo[1] if inrange(year, 2012, 2016)
              ​​​​​​​or possibly doing something like below if possible?

              Code:
              by county (vetsedu_*), sort: replace vets25yo = vetsedu_*[1] if inrange(year, 2012, 2016)

              Comment


              • #22
                So if i had several variables within the same range, would I have to individualize the code? So i have 10 variables with the 2012-2016 range and another 15 in the 2010-2016 range, and so on.

                You would do a loop for the 10 variables with the 2012-2016 range, and another loop for the 15 variables in the 2010-2016 range.

                So why would you recommend the var
                county
                rather than n_county?
                n_county
                was a variable consisting of
                countyname
                and
                statename
                a numeric variable to index the counties.
                I think I wasn't looking at the variable names when I wrote that. Yes, use n_county.

                And if I understand the following correctly:


                Code:
                by county (vets25yo), sort: replace vets25yo = vets25yo[1] if inrange(year, 2012, 2016)
                this would create populate all the data for vets25yo that exist in 2012 and apply it to all the missing spaces in 2013, 2014, 2015, 2016.
                Correct.

                However if i had several variables that were populated for 2012 and i wanted it to do the same thing as
                vets25yo
                would I include all the other variables in the parenthesis? so

                Code:

                by county (vetsedu_hs vetsedu_hsplus ), sort: replace vets25yo = vets25yo[1] if inrange(year, 2012, 2016)
                ​​​​​​​or possibly doing something like below if possible?

                Code:

                by county (vetsedu_*), sort: replace vets25yo = vetsedu_*[1] if inrange(year, 2012, 2016)
                No, the first of these will run without error messages but produce incorrect results. The second will give you syntax error messages.

                You have to loop over the different variables:

                Code:
                foreach x of varlist vetsedu_hs vetsedu_hsplus {
                    by county (`x'), sort: replace `x' = `x'[1] if inrange(year, 2012, 2016)
                }

                Comment

                Working...
                X