Announcement

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

  • Help creating a new variable for age from existing variables

    Basically I have population data from the ACS which gives you different variables for age ranges and the number of people in each county that fits within that range. What I'm trying to do is create a new variable called "age" where the variables "age10to14" is coded as 1, "age15to19" is coded as 2, etc. Here's a picture of my data editor to give you a better idea of what I'm talking about.
    Attached Files

  • #2
    For why screenshots are less help than you hope please read and act on https://www.statalist.org/forums/help#stata

    Sorry, but I don't understand any of this. A single new variable can only take on one value for each observation (county? in USA??). So please tell us how the variable would be calculated.

    Comment


    • #3
      This will work:

      Code:
      gen long county = _n
      
      reshape long age, i(county) j(range) string
      rename age age_count
      encode range, gen(age_group)
      drop range
      Note: If there is already a variable county (or some other variable that identifies the observations uniquely) skip the first line and replace county by that variable in the -i()- option of the -reshape- command.

      If you look at your data in the browser, or list it, it will look like the age_group variable is a string. But it is not. It is an integer variable with values 1, 2, ... that has the corresponding age ranges as value labels.

      In your future posts, please do NOT use screen shots to show data. This one was readable, but many are not. Even when readable, had it been necessary to bring the data into Stata to develop or test the code, that would not be possible from an image. The helpful way to show example data is to use the -dataex- command. Run -ssc install dataex- to install it, and then run -help dataex- to see the simple instructions for using it. Always use it when showing example data: it permits those who want to help you to create a complete and faithful replica of your Stata example with a simple copy/paste operation.

      Also, this is an international, multidisciplinary forum. So avoid using jargon or abbreviations that would not be understood by every reasonably well-educated adult in the world. I suspect many people on this Forum have no idea what the ACS is.

      Comment


      • #4
        I apologize for using a screenshot, I didn't know. However, the reshape command did not work. I already have a county variable so I skipped the first step, like you said, and when I tried to use the second, it says "Your data are currently wide. You are performing a reshape long. You specified i(county) and j(range). In the current wide form, variable county should uniquely identify the observations."

        Comment


        • #5
          OK, the message is self explanatory. Apparently you have more than one observation for every county. Perhaps it is county by year or something like that. Or perhaps it is county by gender by ethnicity or something like that. Anyway, whatever goes in -i()- has to be a variable, or combination of variables, which uniquely identifies observations. Nearly all data sets have some combination of variables like that, by design. If yours does not, then just do
          Code:
          gen long obs_no = _n
          reshape long age, i(obs_no) j(range) string
          rename age age_count
          encode range, gen(age_group)
          drop range

          Comment


          • #6
            Still hoping for a self-explanatory data example (#2), but trying to be Sherlock Holmes on the screenshot I think I see some kind of grouping whereby observations 1, 9, 17, ... have high values, suggesting groups of 8 somehow.

            Good advice will follow a clearer picture of data structure.

            Comment


            • #7
              Cross-posted in different form at https://www.reddit.com/r/stata/comme...bles_into_one/

              I can't follow the dialogue there either.

              Steve: The Statalist home page and the new message prompt ask members to read the FAQ before posting. It's not too late for whatever posts follow.

              To the mention of

              #12 https://www.statalist.org/forums/help#Stata

              I add that

              #8 https://www.statalist.org/forums/help#crossposting

              is pertinent:


              8. May I cross-post to other forums?

              People posting on Statalist may also post the same question on other listservers or in web forums. There is absolutely no rule against doing that.

              But if you do post elsewhere, we ask that you provide cross-references in URL form to searchable archives. That way, people interested in your question can quickly check what has been said elsewhere and avoid posting similar comments. Being open about cross-posting saves everyone time.

              If your question was answered well elsewhere, please post a cross-reference to that answer on Statalist.

              Comment

              Working...
              X