Announcement

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

  • Viewing value labels in a loop

    Hi there,

    I am trying to retrieve the value labels for the encoded variable pos_index (defined below) in a forvalues loop to determine the correlation, split by the four levels of pos_index, between two numerical variables. I wish to display the category before the cross-correlation table for each level but I am unable to do so. Any help would be greatly appreciated.


    Code:
    encode position, gen (pos_index) lab def pos_label 1 "Defenders" 2 "Forwards" 3 "Goalkeepers" 4 "Midfielders" lab val pos_index pos_label forvalues i in 1/4 { display "`i'" pwcorr age caps if pos_index == `i' } This obviously only displays 1,2,3 and 4 but I'm looking for the amendment to this code that would help me display the corresponding value label from pos_index.

    Also as a sidenote, is there any way for me to incorporate in the first line of the forvalues command itself, a reference to the levels of the variable that I would like to loop over?

    Something in the ilk of -

    Code:
    foreach i in levels(pos_index) { display "`i'" pwcorr age caps if pos_index == `i' } This particular code gives me a syntax error but the the correct version of this would allow Stata to directly pick up the distinct levels in the variable of interest as opposed to me explicitly specifying them.

    Note: I am aware of the levelsof(var), local() command but that too only displays the numeric code as opposed to the label.

    Any help would be greatly appreciated.

    Thank you in adavnce,

    Yash

  • #2
    I apologize in advance for the poor formatting of the code. I thought it would turn out correctly formatted but I clearly misinterpretted the instruction. Allow me to repost -


    I am trying to retrieve the value labels for the encoded variable pos_index (defined below) in a forvalues loop to determine the correlation, split by the four levels of pos_index, between two numerical variables. I wish to display the category before the cross-correlation table for each level but I am unable to do so. Any help would be greatly appreciated.

    Code:
    Code:
    encode position, gen (pos_index)
    lab def pos_label 1 "Defenders" 2 "Forwards" 3 "Goalkeepers" 4 "Midfielders"
    lab val pos_index pos_label
    forvalues i in 1/4 {
       display "`i'"
       pwcorr age caps if pos_index == `i'
       }

    This obviously only displays 1,2,3 and 4 but I'm looking for the amendment to this code that would help me display the corresponding value label from pos_index.

    Also as a sidenote, is there any way for me to incorporate in the first line of the forvalues command itself, a reference to the levels of the variable that I would like to loop over?

    Something in the ilk of -

    Code:
    Code:
    foreach i in levels(pos_index) {
       display "`i'"
       pwcorr age caps if pos_index == `i'
       }
    This particular code gives me a syntax error but the the correct version of this would allow Stata to directly pick up the distinct levels in the variable of interest as opposed to me explicitly specifying them.

    Note: I am aware of the levelsof(var), local() command but that too only displays the numeric code as opposed to the label.

    Comment


    • #3
      There is an extended macro function (see: help extended_fcn), label, that obtains the value label for a given integer value. Here is a simple example

      Code:
      sysuse auto
      
      levelsof foreign , local(levels)
      foreach i of local levels {
          local label : label (foreign) `i'
          display "`label'"
      }
      I would also like to point to elabel (SSC) for an alternative route.

      Code:
      * ssc install elabel
      elabel list (foreign)
      return list
      Best
      Daniel

      Comment


      • #4
        Great, that works perfectly.

        Thank you Daniel,

        Yash

        Comment

        Working...
        X