Announcement

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

  • loop to change variable's value

    Hi! My first post here. So, I want to reshape from long to wide. The variables are called cc_know_* and cc_use_* with * ranging between 1-20 with explanation as follow:
    Code:
    1 "pill" 2 "iud" 3 "injection" 4 "diaphragm" 5 "m condom" 6 "f sterilization" 7 "m sterilization" 8 "rhythm" 9 "withdrawal" 10 "other trad" 11 "Norplant or implant" 12 "abstinence" 13 "lactation" 14 "f condom" 15 "foam/jelly" 16 "emerg contra" 17 "other modern method" 18 "standard days method" 19 "country spec 1" 20 "country spec 2"
    Ideally the label of each var is going to be "pill" if the variable is called cc_know_1

    Any suggestion?

  • #2
    My guess is that you want the value labels of one variable to be the variable labels of a set of variables after a reshape wide. If so, there is no question there of changing any values whatsoever.

    You have the definitions there. All you need to do is edit each line so that you have 20 lines like

    Code:
    label var cc_know_1 "pill"
    In my favourite text editor that is a one line command. At worst, it's a few seconds' copy-and-paste after typing

    Code:
    label var cc_know_
    once.

    Then make that into a .do file which you do.

    Then edit all occurrences of

    _know_

    to

    _use_

    in the do file and do again.

    If more information on do-files is needed, start with

    Code:
    help do

    Comment


    • #3
      Hi Nick, thanks for answering. I think I might have created a confusion. So, I want to give labels to the variable. My variables are:
      Code:
      cc_know_1 cc_know_2 cc_know_3
      cc_use_1 cc_use_2 cc_use 3
      The intepretation of the numbers are as I have listed above. And I'd like to change the label using loop. I have tried something like this:

      Code:
       label define cc 1 "pill" 2 "iud" 3 "injection" 4 "diaphragm" 5 "m condom" 6 "f sterilization" 7 "m sterilization" 8 "rhythm" 9 "withdrawal" 10 "other trad" 11 "Norplant or implant" 12 "abstinence" 13 "lactation" 14 "f condom" 15 "foam/jelly" 16 "emerg contra" 17 "other modern method" 18 "standard days method" 19 "country spec 1" 20 "country spec 2", modify
      
      forvalues i = 1(1)20{
          label var cc_know_`i' cc
          label var cc_use_`i' cc
      }
      It didn't give me error, but instead of giving the label correctly, now everything just says "cc". How do I fix this? I know that I can do it manually, but I thought that maybe I can use loop instead? since it can be a learning point for me.

      Comment


      • #4
        No; variable labels and value labels are utterly unrelated. You need code like that already indicated.

        Comment

        Working...
        X