Announcement

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

  • difficulty with forvalues/foreach naming

    hi all, I'm trying to generate a new set of variables and struggling with the naming --

    I have a variable of "state" that has a number assigned to each state, and a variable for 2016 presidential election results. I want to create a new set of variables, one for each state, of the presidential vote share.

    forvalues j = 1/50 {
    generate pres_`j'=.
    replace pres_`j'=pres2016 if state==`j'
    }

    This works, but I want the output to be the state names like pres_alabama, pres_alaska, pres_arizona ...

    Any idea how to do this? Should I be using the foreach command instead of forvalues? All help is greatly appreciated!

  • #2
    Welcome to Statalist.

    Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with your description, and each would require a somewhat different approach. In order to get a helpful response, you need to show some example data.

    Be sure to use the dataex command to do this. If you are running version 15.1 or a fully updated version 14.2, dataex is already part of your official Stata installation. If not, run ssc install dataex to get it. Either way, run help dataex and read the simple instructions for using it. dataex will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    With that said, give a moment's thought to the following. I'm guessing your variable state has numeric values and a value label that causes the numbers to be displayed (for example, by the list command) as the state's name. If that is the case, then it's going to take some extra work, because "pres_New Jersey" cannot be a Stata variable name with that space in the middle.

    But do show a sample of your data using dataex and perhaps someone can work out a solution. Myself, I hate typing long variable names and what I would probably ignore the state name and instead make a list of state postal codes to use: pres_NJ for example.

    Comment


    • #3
      Thanks for your response, super helpful! I plan on doing NJ instead of New Jersey, just was typing out the long form in my question.

      Here's what dataex produced:
      input byte(state pres2016)
      1 2
      1 2
      1 2
      1 1
      1 1
      1 2
      1 2
      1 2
      1 98
      1 2
      1 1
      1 2
      1 1
      1 1
      1 2

      I've figured out I can use decode to get my states in to a string variable, but I'm still struggling with how to do a foreach with a string variable. I've been reading other forum entries, but further help would be greatly appreciated!

      Comment


      • #4
        Here's example code that iterates across the values taken by a string variable.
        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . keep in 1/5
        (69 observations deleted)
        
        . levelsof make, local(makes)
        `"AMC Concord"' `"AMC Pacer"' `"AMC Spirit"' `"Buick Century"' `"Buick Electra"'
        
        . foreach car of local makes {
          2.     display "`car'"
          3. }
        AMC Concord
        AMC Pacer
        AMC Spirit
        Buick Century
        Buick Electra

        Comment

        Working...
        X