Announcement

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

  • Label multiple (string) variables at the same time via foreach

    Hi STATALIST members,

    I am trying to label all my (string) variables that are coded as Yes/No with 0=no, 1=yes. I tried to do this with the code mentioned below (in which replace is mentioned as I already defined yesno in one of the many attempts before). After this code I get the following error: factor-variable and time-series operators not allowed
    r(101);

    label define yesno 0 "no" ///
    1 "yes" , replace
    foreach v of varlist literacy literacycheck firsthusband bankaccount backaccother childmortality planmorechildren foodsecurity1 foodsecurity2 foodsecurity3 foodsecurity4 foodsecurity5 foodsecurity6 foodsecurity7 foodsecurity8 foodsecurity9 Nutrition_1 Nutrition_2 Nutrition_5 Nutrition_6 Nutrition_7 Nutrition_8 Nutrition_9 Nutrition_10 Toiletshare vegetablegarden assetsmatrix1_1 assetsmatrix1_2 assetsmatrix1_3 assetsmatrix1_4 assetsmatrix1_5 assetsmatrix1_6 assetsmatrix2_10 assetsmatrix2_11 assetsmatrix2_12 assetsmatrix2_13 assetsmatrix2_7 assetsmatrix2_9 assetsmatrix2_8 assetsmatrix3_14 assetsmatrix3_15 assetsmatrix3_16 assetsmatrix3_17 assetsmatrix3_18 assetsmatrix3_19 assetsmatrix3_20 assetsmatrix3_21 mosquitonets socialsupport_1 socialsupport_2 socialsupport_3 socialsupport_4 stereotypes_1 stereotypes_2 stereotypes_3 stereotypes_4 debt {
    encode label(yesno) `v', gen(_`v')
    order _`v', before(`v')
    drop `v'
    rename _`v', `v'
    }

    Would be great if someone would know how to deal with this!

    Thank you in advance,

    Linda

  • #2
    Here are a couple of fixes:

    1) As the syntax diagram in -help encode- shows, the variable name is the first thing after "encode," and label() goes after the comma, as an option.
    Code:
    encode `v', gen(_`v') label(yesno)
    2) Per the syntax diagram shown in -help rename- , a comma is not included as part of the rename command syntax:
    Code:
    rename _`v' `v'
    Some other suggestions:
    1) Take a look again at the StataList FAQ aimed at new members. You can learn about showing example data with -dataex- and about putting your code in code delimiters using the "#" menu item, which gives a nice readable presentation.
    2) Really long lines make your code less readable to us and probably you. Many users would do something like this:
    Code:
    foreach v of varlist literacy literacycheck firsthusband bankaccount backaccother ///
       childmortality planmorechildren foodsecurity1 foodsecurity2 foodsecurity3 ///
       foodsecurity4 foodsecurity5 foodsecurity6 foodsecurity7 foodsecurity8 ///
    ...
    ...
    }
    You could also probably "wildcard" many of your varnames, e.g., foodsecurity*, assetsmatrix*, etc., which might also help readability.
    Last edited by Mike Lacy; 16 Jul 2020, 12:17.

    Comment


    • #3
      Dear Mike, thank you for the tips & tricks! The adjustments you suggested worked!

      Will take a look at your reference to the STATALIST FAQ!

      :-)

      Have a good day

      Comment

      Working...
      X