Announcement

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

  • #16
    What you want is not possible. In Stata there are rules about variable names, e.g., they cannot have spaces in, and they cannot start with numbers.

    So if you want to create variables with city names, you need to change the city names.

    Another possibility is to create variables with some names, but to label the variables with the city names. Something like this:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . keep in 1/10
    (64 observations deleted)
    
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . keep in 1/5
    (69 observations deleted)
    
    . keep make
    
    . forvalues i=1/`=_N' {
      2. gen var`i'=.
      3. label var var`i' "`=make[`i']'"
      4. }
    (5 missing values generated)
    (5 missing values generated)
    (5 missing values generated)
    (5 missing values generated)
    (5 missing values generated)
    
    . list, sep(0)
    
         +--------------------------------------------------+
         | make            var1   var2   var3   var4   var5 |
         |--------------------------------------------------|
      1. | AMC Concord        .      .      .      .      . |
      2. | AMC Pacer          .      .      .      .      . |
      3. | AMC Spirit         .      .      .      .      . |
      4. | Buick Century      .      .      .      .      . |
      5. | Buick Electra      .      .      .      .      . |
         +--------------------------------------------------+
    
    . des
    
    Contains data from C:\Program Files (x86)\Stata15\ado\base/a/auto.dta
      obs:             5                          1978 Automobile Data
     vars:             6                          13 Apr 2016 17:45
     size:           190                          (_dta has notes)
    ----------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ----------------------------------------------------------------------------------------------------
    make            str18   %-18s                 Make and Model
    var1            float   %9.0g                 AMC Concord
    var2            float   %9.0g                 AMC Pacer
    var3            float   %9.0g                 AMC Spirit
    var4            float   %9.0g                 Buick Century
    var5            float   %9.0g                 Buick Electra
    ----------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    In the above I generated variables var1-var5, but the names of the cities (in my example of car makes) are in the variable labels.

    It depend on what you want to do next, but you really have no other choice but:

    1. To change city names if you want to call variables like the cities.

    2. Move the city names to the variable labels.
    Originally posted by Konstantina Maragkou View Post
    I was mostly looking for a way to do this without changing the city names, but thanks a lot for the suggestion!

    Comment


    • #17
      Hello all, I am using Stata 16. I am trying to generate a new variable for the state name that corresponds to the state_id in my dataset.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte state_id int year
      1 2015
      1 2020
      1 2025
      1 2030
      2 2015
      2 2020
      2 2025
      2 2030
      3 2015
      3 2020
      3 2025
      3 2030
      4 2015
      4 2020
      4 2025
      4 2030
      
      end
      label values state_id statename
      I want to assign names to them as 1- aaa 2-bbb 3-ccc 4- ddd respectively

      I tried the following code
      Code:
      set obs 72
      foreach var of varlist state_id {
      replace `var'="aaa" if `var'=="1"
      replace `var'="bbb" if `var'=="2"
      replace `var'="ccc" if `var'=="3"
      replace `var'="ddd" if `var'=="4"
      }
      I get an error type mismatch. Kindly suggest where I am going wrong. Thank you, grateful for any help.

      Comment


      • #18
        state_id is numeric. So you can't check for equality with string values and you can't assign string values to it. Both actions fail as type mismatches.

        What you want might be a string variable so that this code for example is legal. Note that the loop using foreach does no harm but is not needed.

        Code:
        gen state_name = ""  replace state_name = "aaa" if state_id == 1  replace state_name = "bbb" if state_id == 2
        What is usually a better idea is to define value labels. See the help for label and linked manual entries.

        Comment

        Working...
        X