Announcement

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

  • Rename variables

    I am trying to replace my variables with names in first row that seem to be long.
    I want to first derive the first 14 characters in the string in row 1, then use the returned string to replace the variable names.
    When I do that, I get an error "variable newvars not found".
    Kindly assist
    Here is my code:
    Code:
    foreach var of varlist * {
        local names "`=`var'[1]'"
        foreach x of local names{
            replace `x' = substr(`x', 1, 14)
        }
    }
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 _varname str49 _var1 str44 _var2 str55 _var3 str56 _var4
    "newvars"        "seed_yam_multiplication_tech_Bono_East_Oti_farmer" "seedbed_options_ridging_Bono_East_Oti_farmer" "stress_tolerant_improved_varieties_Bono_East_Oti_farmer" "disease_and_pest_tolerant_varieties_Bono_East_Oti_farmer"
    "cs_score_prod"  "1"                                                 "4"                                            "4"                                                       "4"                                                       
    "cs_score_adapt" "3"                                                 "3"                                            "4"                                                       "3"                                                       
    end

  • #2
    The immediate bug is that you needed to write

    Code:
    substr("`x'", 1, 14)
    as you want to work on the name, not the value(s) variable.

    Also, renaming requires
    rename. .

    Finally, metadata in observation 1 just get in the way of what you need to do.

    What I imagine that you intend can be reduced to a single loop:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 _varname str49 _var1 str44 _var2 str55 _var3 str56 _var4
    "newvars"        "seed_yam_multiplication_tech_Bono_East_Oti_farmer" "seedbed_options_ridging_Bono_East_Oti_farmer" "stress_tolerant_improved_varieties_Bono_East_Oti_farmer" "disease_and_pest_tolerant_varieties_Bono_East_Oti_farmer"
    "cs_score_prod"  "1"                                                 "4"                                            "4"                                                       "4"                                                       
    "cs_score_adapt" "3"                                                 "3"                                            "4"                                                       "3"                                                       
    end
    
    foreach var of varlist * {
        local newname = substr(`var'[1], 1, 14) 
        rename `var' `newname'
    }
    
    drop in 1 
    
    destring *, replace 
    
    describe
    
    list 
    
    
    . describe
    
    Contains data
     Observations:             2                  
        Variables:             5                  
    ---------------------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ---------------------------------------------------------------------------------------------------------------
    newvars         str18   %18s                  
    seed_yam_multi  byte    %10.0g                
    seedbed_option  byte    %10.0g                
    stress_toleran  byte    %10.0g                
    disease_and_pe  byte    %10.0g                
    ---------------------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    
    . 
    . list 
    
         +------------------------------------------------------------+
         |        newvars   seed_y~i   seedbe~n   stress~n   diseas~e |
         |------------------------------------------------------------|
      1. |  cs_score_prod          1          4          4          4 |
      2. | cs_score_adapt          3          3          4          3 |
         +------------------------------------------------------------+
    Code:
    
    


    Last edited by Nick Cox; 29 Mar 2022, 04:28.

    Comment


    • #3
      Wait, do you want to rename the variable or change the contents of it? These are very different concepts.

      Comment


      • #4

        Jared Greathouse I think Co Ar does want to rename, but sees replace as the first step towards that. See #2 for how you can get straight there.

        Comment


        • #5
          Yes, before you edited it I couldn't quite follow, but yes now this makes sense

          Comment


          • #6
            Jared Greathouse I want to rename the variable
            Nick Cox thanks for pointing out the bug
            I have modified the code as follows and now gets the error "newvars not allowed"

            Code:
            foreach var of varlist * {
                local names: `=`var'[1]'
                foreach x of local names{
                    replace `x' = substr("`x'", 1, 14)
                    rename `var' "`=`x'[1]'"
                    drop in 1
                    destring, replace
                }
            }

            Comment


            • #7
              Sorry, but I think my code is better. It also works.

              Indeed, your revised code is quite wrong, as each time around the loop you drop the first observation and thus destroy what you want to use.

              Also, there is zero need to make this a nested loop.

              Please look again. at the logic.


              Code:
              for each variable
                  get a new name
                  rename the variable
              
              NOW drop the first observation (just once)
              
              NOW destring everything.
              Last edited by Nick Cox; 29 Mar 2022, 04:46.

              Comment


              • #8
                Thanks Nick Cox that worked.

                Comment


                • #9
                  Thanks Nick Cox that worked

                  Comment


                  • #10
                    By the way Nick Cox what is the difference between
                    Code:
                    `var'[1]
                    and
                    Code:
                    "`=`var'[1]'"
                    in plain English. Sorry if I asked for much. Or would someone please point me to a resource where I can read more about this.
                    Last edited by Co Ar; 29 Mar 2022, 07:17.

                    Comment


                    • #11
                      The second insists that a value is treated as a literal string. The first does not. If the variable is numeric, the difference is a big deal, but not otherwise.

                      My main reason for writing

                      Code:
                      local newname = substr(`var'[1], 1, 14)
                      rather than
                      Code:
                        
                       local newname = substr("`= `var'[1]'", 1, 14)
                      is that the first is simpler.



                      Comment


                      • #12
                        Thank you so much Nick Cox

                        Comment

                        Working...
                        X