Announcement

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

  • How do I rename suffixes that aren't fully displayed due to variable name limit? -rensfix -

    Hello everyon,
    I have two questions:

    I have imported a .sav file. My variable names have all been truncated to 19 characters. How do I increase this?

    I am using the very helpful user-written (by Nick Cox) ado called -rensfix-. The problem I am having is that as the variable names are truncated (all vars had the same suffix _respiratory before importing), I can't chance them all in one go to _1. Here is an example of 4 truncations of the same stub:


    SpecifyTTTAttacks_r
    ShortExer_respirato
    CoughCharac_respira
    CoughExer_respirato
    AgePneumonia_respir

    Is there a way of changing them all to _1 without having to go through every suffix?

    I was hoping something like rensfix _respi* 1 would work!

  • #2
    Sorry, but I have no idea what a .sav file is.

    I will answer the second question.


    rensfix
    is by Stephen Jenkins and myself. You're asked to indicate provenance (where), not author (who), for user-written programs. You have to dig for this one:

    . search rensfix, sj

    Search of official help files, FAQs, Examples, SJs, and STBs

    (end of search)

    . search rensfix, sj historical

    Search of official help files, FAQs, Examples, SJs, and STBs

    STB-59 dm83 . . . . . . . . . . . . . Renaming variables: changing suffixes
    (help rensfix if installed) . . . . . . . S. P. Jenkins and N. J. Cox
    1/01 pp.5--6; STB Reprints Vol 10, pp.34--35
    renames variables by changing the suffix
    Either way, that command has long since superseded by other stuff. Here is a case in point.

    You can specify variable names with a wildcard but not suffixes.

    A data example would have helped here, but here we go.

    Code:
    . clear
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . foreach v in SpecifyTTTAttacks_r ShortExer_respirato CoughCharac_respira CoughExer_respirato AgePneumonia_respir {
      2.         gen `v' = 42
      3. }
    
    .
    . ds, varwidth(20)
    SpecifyTTTAttacks_r  CoughCharac_respira  AgePneumonia_respir
    ShortExer_respirato  CoughExer_respirato
    
    .
    . rename (*_*) (*_1)
    
    .
    . ds, varwidth(20)
    SpecifyTTTAttacks_1  CoughCharac_1        AgePneumonia_1
    ShortExer_1          CoughExer_1




    Last edited by Nick Cox; 21 Nov 2016, 05:25.

    Comment


    • #3
      Thank you so much!
      rename (*_*) (*_1) worked perfectly and so it was so simple.

      Comment


      • #4
        Since you mentioned a .sav file, I assume the file was under SPSS package. You may also transform the .sav into .csv and import to Stata, and see what happens.


        Anyway, below, there is a short example on how to deal with a long variable name as well as sufix and prefix:

        Code:
        . list
        
             +-------------------------------------------+
             | thisis~1   thisis~2   anothe~1   anothe~4 |
             |-------------------------------------------|
          1. |        2          5          6          8 |
          2. |        3          6          7          9 |
          3. |        4          6          7          8 |
          4. |        5          6          7          5 |
          5. |        6          7          5          4 |
             |-------------------------------------------|
          6. |        7          8          6          1 |
             +-------------------------------------------+
        
        . list this*
        
             +---------------------+
             | thisis~1   thisis~2 |
             |---------------------|
          1. |        2          5 |
          2. |        3          6 |
          3. |        4          6 |
          4. |        5          6 |
          5. |        6          7 |
             |---------------------|
          6. |        7          8 |
             +---------------------+
        
        . list this*, abb(40)
        
             +---------------------------------------------------------------------+
             | thisisaverylongnameforavariable1   thisisaverylongnameforavariable2 |
             |---------------------------------------------------------------------|
          1. |                                2                                  5 |
          2. |                                3                                  6 |
          3. |                                4                                  6 |
          4. |                                5                                  6 |
          5. |                                6                                  7 |
             |---------------------------------------------------------------------|
          6. |                                7                                  8 |
             +---------------------------------------------------------------------+
        
        . list another*, abb(40)
        
             +-------------------------------------------------------------+
             | anothervariablewithlongname1   anothervariablewithlongname4 |
             |-------------------------------------------------------------|
          1. |                            6                              8 |
          2. |                            7                              9 |
          3. |                            7                              8 |
          4. |                            7                              5 |
          5. |                            5                              4 |
             |-------------------------------------------------------------|
          6. |                            6                              1 |
             +-------------------------------------------------------------+
        
        . rename (this*# another*#) v#, addnumber
        
        . list
        
             +-------------------+
             | v1   v2   v3   v4 |
             |-------------------|
          1. |  2    5    6    8 |
          2. |  3    6    7    9 |
          3. |  4    6    7    8 |
          4. |  5    6    7    5 |
          5. |  6    7    5    4 |
             |-------------------|
          6. |  7    8    6    1 |
             +-------------------+
        Hopefully that helps.

        Best,

        Marcos
        Best regards,

        Marcos

        Comment


        • #5
          Very helpful Marcos! Thank you. I have another related question, if you don't mind. So, I have solved the original variable name problem but how do I get rid of prefixes and suffixes in the labels so that only the middle portion renames (e.g., I only want CoughExer in the first label below) :

          . lab dir
          DataResponse_CoughExer_respirato
          DataResponse_CoughCharac_respira
          DataResponse_ShortExer_respirato
          DataResponse_Bronchiolitis_respi
          DataResponse_CongColds_respirato
          DataResponse_EverHosp_respirator
          DataResponse_ColdsPerY_respirato
          DataResponse_CAsthmaMed_respirat
          DataResponse_CAsthma_respiratory
          Last edited by Sara Khan; 21 Nov 2016, 07:02.

          Comment


          • #6
            See
            Code:
            h rename group
            Code:
            rename DataResponse_*_res* *

            Comment

            Working...
            X