Announcement

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

  • Renaming variables in reverse chronological order

    Hello,

    I am trying to rename variables in reverse order. I am running Stata 15.1
    I appreciate everyone's help and patience as I work with Stata.

    Is there a way to make this count down, from 18 to 1, instead of from 1 to 18?

    I've ran this so far and it adds from 1 to 18:

    import delimited using C:\Users\Mike\Desktop\Stata\Demographics\1.csv, clear
    rename v1 school
    rename v2 state
    rename (v3-v20) ptd=
    rename ptdv* ptd*
    rename ptd# ptd#, renumber(1)

    Thank you.

  • #2
    I wouldn't recommend suffixes 18 to 1 as they won't sort in the right order and wildcarding is more awkward. This is a case where I would write a loop to do the harder part and then (perhaps) try to think up a smarter way to do it in one with rename.

    Assuming that ptd1 to ptd18 should be ptd18 to ptd01

    Code:
    forval j = 1/18 {
         local J : di %02.0f 19 - `j'
         rename ptd`j' whatever`J'
    }
    rename whatever* ptd*

    Comment


    • #3
      Another way to go, whereas a loop is still utilized.
      Code:
      forval i=1/18 {
      order ptd`i'
      }
      ren ptd# ptd#, renumber

      Comment


      • #4
        I'd repeat my advice to use ptd01 to ptd18. Suffixes that are more meaningful always beat those that are less meaningful.

        Comment


        • #5
          Nick's advice is meaningful: without the appropriate 2-digit suffixes, any order of variables would require absolute caution.
          Code:
          forval i=1/18 {
          order ptd`i'
          }
          ren ptd# ptd(##), renumber
          Anyhow, there is still an exceptional case, whereas 1-digit suffixes are serving for -reshape- if you might need it later on.
          Last edited by Romalpa Akzo; 21 Aug 2018, 11:10.

          Comment


          • #6
            Variable name suffixes that are 2 or more digits starting 01, or 001, or 0001, ... are fine for reshape. Just use the string option and leading zeros will be copied as such.

            Comment


            • #7
              Thank you for your help Nick and Romalpa.

              For adding more variables, say from v21-38, i want to add fle18 to fle01, would I close the loop or continue adding onto it?

              Comment


              • #8
                If you want v21 to v38 to map to fle18 to fle01 that could be another loop. It's not the same loop extended.

                Comment


                • #9
                  Thank you both!

                  Comment


                  • #10
                    Hi,

                    I'm inputting this:

                    forval j = 1/18 {
                    local J : di %02.0f 19 - `j'
                    rename fle`j' whatever`J'
                    }
                    rename whatever* fle*


                    Once I run the second loop, it renames fle18 to fle01, but renames all the variables before the loop back to v1, v2, etc.

                    Comment


                    • #11
                      I can't see any reason why that code would do that. Please provide a reproducible example and error report.

                      Comment


                      • #12
                        Here is what I'm running. It is fine with the first csv but the second csv causes issues to revert back to v3, v4, etc.

                        log using C:\Users\Mike\Desktop\Stata\Charter.log, replace

                        import delimited using C:\Users\Mike\Desktop\Stata\Demographics\1.csv, clear
                        rename v1 school
                        rename v2 state
                        rename (v3-v20) ptd=
                        rename ptdv* ptd*
                        rename ptd# ptd#, renumber(1)
                        forval j = 1/18 {
                        local J : di %02.0f 19 - `j'
                        rename ptd`j' whatever`J'
                        }
                        rename whatever* ptd*
                        sort school
                        sort state
                        duplicates drop school state, force
                        save d:datadata1_a, replace

                        import delimited using C:\Users\Mike\Desktop\Stata\Demographics\2.csv, clear
                        rename v1 school
                        rename v2 state
                        rename (v3-v20) g1aim=
                        rename g1aimv* g1aim*
                        rename g1aim# g1aim#, renumber(1)
                        forval j = 1/18 {
                        local J : di %02.0f 19 - `j'
                        rename g1aim`j' whatever`J'
                        }
                        rename whatever* g1aim*
                        rename (v21-v38) g1aif=
                        rename g1aifv* g1aif*
                        rename g1aif# g1aif#, renumber(1)
                        forval j = 1/18 {
                        local J : di %02.0f 19 - `j'
                        rename g1aif`j' whatever`J'
                        }
                        rename whatever* g1aif*
                        sort school
                        sort state
                        duplicates drop school state, force
                        save d:datadata2_a, replace
                        merge 1:1 school state using "D:\datadata1_a.dta"
                        drop _merge
                        save d:datadata2_a, replace

                        Comment


                        • #13
                          Really not reproducible. Simply, I can't access your computer. I am in meetings for a while but I'll see if later staring at the code allows further comment.

                          Advice at https://stackoverflow.com/help/mcve is pertinent.

                          Comment


                          • #14
                            This is reproducible code yet does not show a problem. I edited your code down a fair bit. Perhaps I edited out a bug, but I didn't sense one in your code.

                            Code:
                            clear 
                            set obs 1 
                            forval j = 1/38 { 
                                gen v`j' = 42 
                            } 
                            
                            * block 1 
                            rename (v1 v2) (school state) 
                            
                            * block 2 
                            rename (v3-v20) g1aim#, addnumber
                            
                            forval j = 1/18 {
                                local J : di %02.0f 19 - `j'
                                rename g1aim`j' whatever`J'
                            }
                            rename whatever* g1aim*
                            
                            * block 3 
                            rename (v21-v38) g1aif#, addnumber 
                            
                            forval j = 1/18 {
                                local J : di %02.0f 19 - `j'
                                rename g1aif`j' whatever`J'
                            }
                            
                            rename whatever* g1aif*
                            
                            * did it work? 
                            
                            ds s* 
                            ds g1aim* 
                            ds g1aif*
                            Results:

                            Code:
                            . ds s* 
                            school  state
                            
                            . ds g1aim* 
                            g1aim18  g1aim16  g1aim14  g1aim12  g1aim10  g1aim08  g1aim06  g1aim04  g1aim02
                            g1aim17  g1aim15  g1aim13  g1aim11  g1aim09  g1aim07  g1aim05  g1aim03  g1aim01
                            
                            . ds g1aif* 
                            g1aif18  g1aif16  g1aif14  g1aif12  g1aif10  g1aif08  g1aif06  g1aif04  g1aif02
                            g1aif17  g1aif15  g1aif13  g1aif11  g1aif09  g1aif07  g1aif05  g1aif03  g1aif01

                            Comment


                            • #15
                              That ran great, thank you!

                              Finally, can you explain the string option I would add to my reshape command that you spoke earlier in the forum?

                              Thanks again.

                              Comment

                              Working...
                              X