Announcement

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

  • Creating new variables using forval

    Ciao,

    Here is a question about using forval. Say I administer a test that has 10 different version (1 through 10). Then, say I have a variable for which test is administered that is called test_num and goes from 1 to 10. Say I want to create a dummy variable called test_1 if the test_num ==1 and that I want to do this for all values of test_num. So basically I want to create an indicator variable (1, 0) for each test version. How can I do this using forval? Below is what I tried but it just generated one variable called test_num equal to 1. Thanks so much.
    Code:
    forval test_num=1/10 {
       gen test_`test_num' = 1 if `test_num' = `test_num'
    replace test_`test_num' = 0 if `test_num' != `test_num'
    }
    Last edited by sladmin; 17 Oct 2019, 08:16. Reason: anonymize original poster

  • #2
    Guest, you wrote
    Say I want to create a dummy variable called test_1 if the test_num ==1
    with this definition your dummy variable doesn't vary. It is either always 0 or always 1 (within a test).
    So perhaps you've meant something else.
    Last edited by sladmin; 17 Oct 2019, 08:16. Reason: anonymize original poster

    Comment


    • #3
      Originally posted by Sergiy Radyakin View Post
      Guest, you wrote

      with this definition your dummy variable doesn't vary. It is either always 0 or always 1 (within a test).
      So perhaps you've meant something else.
      Thanks for your comment. i updated the post but the issue you raise is because I don't know how to do this in Stata while I do in fact want to create multiple indicator variables. Thank you!
      Last edited by sladmin; 17 Oct 2019, 08:16. Reason: anonymize original poster

      Comment


      • #4
        You don't need "if", as a comparison operator always yields 0 (false) or 1 (true). But just so that you write it correctly next time, equality is tested with == (for instance, "gen y=1 if x==0").

        Also, your code is misleading because you give the same name to variable test_num and loop "variable" (actually a local macro) test_num. That's why you don't see that `test_num' = `test_num' is not correct: there is the same thing on the left and on the right. You should write test_num == `test_num' instead (quotes are only for macros). But I suggest using a different name:

        Code:
        forv i=1/10 {
            gen test_`i' = test_num==`i'
        }
        Hope this helps

        Jean-Claude Arbaut
        Last edited by Jean-Claude Arbaut; 17 May 2018, 17:03.

        Comment


        • #5
          Because it needs to be said ...

          Perhaps you are generating indicator (dummy) variables because you plan on using them as variables in a regression or other statistical procedure.

          If so, then you should not manually create dummy variables. Instead, you will find factor variable notation a powerful tool in your work. If you are not already familiar with them, do read the output of help factor variables and section 11.4.3 of the Stata User's Guide PDF included with your Stata installation and accessible from Stata's Help menu. Your effort will be amply repaid.

          In particular, the very powerful margins command relies on models being built using factor variables so that it "knows" that if the test 7 dummy increase from 0 to 1, then all other dummies must be, or become, zero. Using margins after building a model using manually created dummy variables puts you at high risk of obtaining incorrect results.

          Comment


          • #6
            William's remark reminds me of another possibility. Of course he is right, you should use factor variables with the i. syntax whenever possible.

            In case you really need to create these dummies, you can use xi instead of the previous loop:

            Code:
            xi, noomit i.test_num
            rename _Itest_num* test*

            Comment

            Working...
            X