Announcement

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

  • Loop for renaming diverse variables?

    Hello,

    does anyone know a way, to write this in short? (via foreach?)


    label variable Volksmusik "Volksmusik hören"

    label variable Volksmusik_anderer_Kulturen "Volksmusik anderer Kulturen hören"

    label variable Schlager "Schlager hören"

    label variable Pop "Pop hören"

    label variable Rock "Rock hören"

    label variable Heavy_Metal "Heavy Metal hören"

    label variable Elektro "Elektro hören"

    label variable HipHopSoulFunk "Hip Hop, Soul, Reaggae hören"

    label variable Klassik "Klassische Musik hören"

    label variable Oper "Oper hören"

    label variable Musical "Musical hören"

    label variable Jazz "Jazz hören"

    It looks a bit stupid and long in my do-file I guess... and I need this operation a few more times (renaming a bunch of variables).
    Btw: Volksmusik-Jazz are in a row. So this might help?! These are variables, which describe musical preferences of interviewees (Likert Scale).

    It is on writing my master thesis on the impact of "Education" and "Social Backround" on music taste.


    Language of labels is German, so don't wonder.


    thx in advance



    PS:

    This is the whole code for creating the "musical preferences"-variables from the Data. Maybe one could write it even shorter in whole

    * define missings

    mvdecode V58-V69, mv(9=.a)




    * lopp for genres-variables:

    foreach x of varlist V58-V69 {

    recode `x' 5=-2 4=-1 3=0 2=1 1=2

    }




    local x 57

    local Musikgenres Volksmusik Volksmusik_anderer_Kulturen Schlager Pop Rock Heavy_Metal Elektro HipHopSoulFunk Klassik Oper Musical Jazz

    foreach m of local Musikgenres {

    local x = `x' + 1

    gen `m' = V`x'

    }




    label define Cluster -2"sehr ungern" 2"sehr gern"

    foreach x of varlist Volksmusik-Jazz {

    label values `x' Cluster

    }




    label variable Volksmusik "Volksmusik hören"

    label variable Volksmusik_anderer_Kulturen "Volksmusik anderer Kulturen hören"

    label variable Schlager "Schlager hören"

    label variable Pop "Pop hören"

    label variable Rock "Rock hören"

    label variable Heavy_Metal "Heavy Metal hören"

    label variable Elektro "Elektro hören"

    label variable HipHopSoulFunk "Hip Hop, Soul, Reaggae hören"

    label variable Klassik "Klassische Musik hören"

    label variable Oper "Oper hören"

    label variable Musical "Musical hören"

    label variable Jazz "Jazz hören"
    Last edited by Florian Kraus; 09 Jun 2018, 04:59.

  • #2
    Welcome to Statalist.

    Were the original variables V58-V69 labelled with the labels you show? If so, the following (untested) changes to your code might solve your problem, by renaming the original variables rather than generating new variables. If not, there is nothing to be gained by a loop, and much to be preferred in having it easy to confirm that the labels are attached to the correct variables.
    Code:
    mvdecode V58-V69, mv(9=.a)
    
    recode V58-V69 5=-2 4=-1 3=0 2=1 1=2
    
    label define Cluster -2 "sehr ungern" 2 "sehr gern"
    label values V58-V69 Cluster
    
    rename (V58-V69) (Musikgenres Volksmusik Volksmusik_anderer_Kulturen Schlager Pop Rock Heavy_Metal Elektro HipHopSoulFunk Klassik Oper Musical Jazz)
    With that said, to improve your future posts, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE] (the "#" on the Advanced Editor Toolbar in Statalist), and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    Comment


    • #3
      No, the variables V58-V69 are not labelled the way I showed in the first place.
      I also want to generate another sets of variables from V58-V69. So I would prefer to generate new variables each time.
      (I also generate Dummies out of V58-V69 later on, for example).

      But thank you, first of all. And sorry for not paying close attention to the FAQ.

      So I think I get it: I cannot label several Variables in a row, but it would work to rename several variables in a row.
      (I was expressing myself misleadingly in my first post, I guess.)


      e: I found a way to do what I want on following page:
      https://sites.google.com/site/imirki...ables-in-stata (Method 11)

      I used following code, which works, after installing "labutil12":
      Code:
       ssc install labutil2
      
      labvars v1 v2 v3 "First label" "Second label" "Third label"
      
      labvars v1 "First label" v2 "Second label" v3 "Third label", alternate


      All in all, this is what my code looks now:

      Code:
      * define missings
      
      mvdecode V58-V69, mv(9=.a)
      
      * loop:
      
      foreach x of varlist V58-V69 {
      
      recode `x' 5=-2 4=-1 3=0 2=1 1=2
      
      }
      
      
      
      local x 57
      
      local Musikgenres Volksmusik Volksmusik_anderer_Kulturen Schlager Pop Rock Heavy_Metal Elektro HipHopSoulFunk Klassik Oper Musical Jazz
      
      foreach m of local Musikgenres {
      
      local x = `x' + 1
      
      gen `m' = V`x'
      
      }
      
      
      label define Cluster -2"sehr ungern" 2"sehr gern" 
      
      foreach x of varlist Volksmusik-Jazz {
      
      label values `x' Cluster
      
      }
      
      
      
      ssc install labutil2 /* labutil12 installieren, um das gleichzeitige Labeln mehrerer Variablen zu ermöglichen */
      
      labvars Volksmusik Volksmusik_anderer_Kulturen Schlager Pop Rock Heavy_Metal Elektro HipHopSoulFunk Klassik Oper Musical Jazz ///
      
      "Volksmusik hören" "Volksmusik anderer Kulturen hören" "Schlager hören" "Pop hören" "Rock hören" "Heavy Metal hören"  ///
      
      "Elektro hören" "Hip Hop, Soul, Reaggae hören" "Klassische Musik hören" "Oper hören" "Musical hören" "Jazz hören"
      Last edited by Florian Kraus; 10 Jun 2018, 03:39.

      Comment

      Working...
      X