Announcement

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

  • Problem in creating a string variable

    Hi dear profs and colleagues,

    I am going to create the native_districts_constant variable. The variable is "metro".
    I use this code:
    Code:
    label define  metroo 1  "Centro-Lisboa" 0 "other"
    label values metro metroo
    decode metro, gen(metarea_str)
    replace metarea_str=proper( metarea_str)
    split metarea_str, p(,)
    drop metarea_str
    replace metarea_str =upper(metarea_str)
    egen metarea_str=concat(metarea_str1 metarea_str2), p(",")
    BUT it says:variable metarea_str2 not found, it is supposed to be but is not. Could anyone have a look at my code to see what is missing? Thank you so much.
    Code:
     egen metarea_str=concat(metarea_str1 metarea_str2), p(",")
    variable metarea_str2 not found
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float metro
    1
    1
    0
    1
    1
    1
    0
    1
    0
    1
    0
    1
    0
    1
    1
    0
    0
    0
    1
    0
    1
    1
    0
    1
    0
    1
    1
    1
    1
    0
    1
    0
    0
    1
    0
    end
    label values metro metroo
    label def metroo 0 "other", modify
    label def metroo 1 "Centro-Lisboa", modify
    Cheers,
    Paris

  • #2
    Paris:
    actually, running your code, I was not able to find out -metarea_str2-:


    Code:
    . set obs 2
    Number of observations (_N) was 0, now 2.
    
    . g metroo=_n-1
    
    . label define  metroo 1  "Centro-Lisboa" 0 "other"
    
    . label values metro metroo
    
    . decode metro, gen(metarea_str)
    
    . replace metarea_str=proper( metarea_str)
    
    . drop metarea_str
    
    . replace metarea_str =upper(metarea_str)
    
    . list
    
         +-------------------------------+
         |        metroo    metarea_str1 |
         |-------------------------------|
      1. |         other           OTHER |
      2. | Centro-Lisboa   CENTRO-LISBOA |
         +-------------------------------+
    
    .
    Last edited by Carlo Lazzaro; 13 Feb 2023, 06:54.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,

      Thank you for getting back to me.
      You are right. I don't know why I thought that should exist --metarea_str2.
      I run yours:

      Code:
       
       drop metarea_str  replace metarea_str =upper(metarea_str)
      variable metarea_str not found

      Comment


      • #4
        Paris:
        this is your turn to be right !
        Just drop the following chunk of code:
        Code:
         drop metarea_str
        and things should work out fine.
        Last edited by Carlo Lazzaro; 13 Feb 2023, 07:21.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          That worked perfectly.
          All my thanks Prof Carlo.

          Comment

          Working...
          X