Announcement

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

  • -rename group- adding alphabeticals?

    Hi all,

    I am trying to transform variable blocks like:

    v25_1 v25_2 v25_3 [...] v25_26 to
    v25a v25b [...] v25z

    using various combinations of wildcards to add the alphabaticals (like Rule 15 in the help- "15. rename v# (a b c)")

    Code:
    rename q28_# (q28a q28b q28c q28d q28e q28f), sort dryrun
    Is there syntax to have just sequential letters added without specifying them explicitly? Seems like there has to be a better solution I'm just not seeing!
    __________________________________________________ __
    Assistant Professor, Department of Biostatistics and Epidemiology
    School of Public Health and Health Sciences
    University of Massachusetts- Amherst

  • #2
    Code:
    clear
    set obs 100
    forval n = 1/26 {
        g v25_`n'  = runiform()
        }
    
    foreach a in `c(alpha)' {
        loc li `"`li' v25`a'"'
        }
        di `"`li'"'
        
    rename v25_# (`li'), sort dryrun
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Using the alpha characters in `c(alpha)' in the rename option works to rename the variable sequentially, but it won't add the 'v25_" prefix in the same line of code as far as I can tell, you can also approach this by running the rename for the alpha characters first and then add the prefix (assumes these are the only variables in your data, you'll need to adjust to ignore other (similarly named) variables)

      Code:
      **without the loop
      rename v25_# (`c(alpha)'),  
      rename * v25*,  
      desc
      Last edited by eric_a_booth; 05 Feb 2017, 09:52.
      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment


      • #4
        Thanks Eric, the (`c(alpha)') bit was just what I was looking for. Unfortunately, -rename- errors out if there are < 26 elements here, so looks like I'll just have to do it explicitly. Would be best to just add a loop to count my elements, but the dataset has all sorts of oddities that would get pulled in I fear.
        __________________________________________________ __
        Assistant Professor, Department of Biostatistics and Epidemiology
        School of Public Health and Health Sciences
        University of Massachusetts- Amherst

        Comment

        Working...
        X