Announcement

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

  • label variable question and substr

    Hi, I have several variables and their names are: q11r1_facebook q11r2_instagram q11r3_twitter q11r4_snapchat q11r5_pinterest q11r6_tiktok q11r7_linkedin q11r8_strava

    I want to do
    Code:
    label variable q11r1_facebook "facebook"
    . (The reason is that when I form the correlation table or regression table, I can read var labels.)

    So I want to do:
    Code:
    foreach v of varlist q11r1_facebook-q11r8_strava {
        label variable `v' "???"
    }
    I guess it should use substr. Can anyone help me?

  • #2
    Correct in so far that is a good way to do it.

    Code:
    foreach v of varlist q11r1_facebook-q11r8_strava {    
        local wanted = substr("`v'", strpos("`v'", "_")+ 1, .)      
        label variable `v' "`wanted'"
    }

    Comment


    • #3
      Great, thanks! Could you tell me what the meaning of +1 is?

      Comment


      • #4
        + signifies addition. Suppose I have 2. Then I write 2 + 1. The meaning is 3.

        I guess you know this really, So why here?

        In this case, the function strpos() finds the position of the underscore, but then you should add 1 because you want to start with the character after the underscore.

        Comment

        Working...
        X