Announcement

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

  • generating a variable derived from multiple expressions

    Elementary question form a PhD student here...

    I'm working on creating a variable derived with logic from 4 other variables with this code which follows a flowchart for this instrument which should ultimately be a 1 or 0:
    generate CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )

    How can I get 'CAMICU' to equal 0 if the above criteria are not met? I've tried the below using 'else'

    . generate CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 ), else CAMICU = 0

    . generate CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 ), else {if CAMICU != 1, CAMICU = 1}

    . generate CAMICU = 1 if {cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )} else CAMICU = 0

    . generate CAMICU = 1 if (cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )) else CAMICU = 0

    . generate CAMICU = 1 if {cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )} else CAMICU == 0

    . generate CAMICU = 1 if {cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )} else = 0

    . generate CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_nu
    > meric >= 4 )
    (805 missing values generated)

    . else {
    . CAMICU = 0 if cam_f1 != 1 | cam_f2_numeric < 8 | ( cam_f3_numeric = 0 | cam_f4_numeric < 4 )
    command CAMICU is unrecognized
    r(199);
    . }


    I'm using Stata/IC 14.2 on Windows. Thanks to everyone who got this far and is considering a reply!

    Tom


  • #2
    Use a logical expression:
    Code:
    generate CAMICU =  (cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 ))
    Just remove the -1 if- from the command and you will have it. By the way, the outermost parentheses are not really needed here, but I find it easier to read the commands when they are used.

    Stata has an -else- command, but its syntax is nothing like what you have shown, and it also doesn't do what you would want it to do. It's very different and, in my experience at least, relatively infrequently used.

    For reasons I can't understand, although it is clearly documented in the PDF manuals that come installed with Stata, logical expressions seem to be a well-kept secret. They are, without doubt, the best way to generate dichotomous variables. Yet scarcely a day goes by on Statalist where we do not see people using unnecessarily complicated code to generate such variables, often getting it wrong, to boot.

    Comment


    • #3
      Let me a few things to what Clyde wrote.

      There are times when you can't easily express what you want as a single expression, and in that case the combination of generate and replace may help.

      While Clyde's code in post #2 is the best approach for your problem, to use your problem as an example for the generate/replace approach we have
      Code:
      generate CAMICU =  0
      replace CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 )
      or even, starting from what you had written
      Code:
      generate CAMICU = 1 if cam_f1 == 1 & cam_f2_numeric >= 8 & ( cam_f3_numeric != 0 | cam_f4_numeric >= 4 ) 
      replace CAMICU = 0 if CAMICU = .
      since, as you must have seen when you ran your code, when a generate command has an if clause, all the observations that do not satisfy the if clause receive a missing value.

      You also need to be aware of the difference between the if clause used on your generate command, which is evaluated for each observation separately
      Code:
      help if
      and the if command
      Code:
      help ifcmd
      which is evaluated once, and the command following it is run or not run.

      The if command can be followed by an else command; the if clause cannot.

      I'm sympathetic to you as a new user of Stata - there is quite a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

      When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      The objective in doing the reading was not so much to master Stata - I'm still far from that goal - as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

      Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

      Stata also supples YouTube videos, if that's your thing.

      Comment


      • #4
        Clyde & William,
        Thank you for sharing your expertise. This was very helpful!
        Tom

        Comment

        Working...
        X