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

  • Generate a new variable based on conditions and if statements

    Hi All,

    Hopefully someone could kindly help me with this problem.

    I want to create a new variable called "VALID" and the way this variable would be coded would depend on two existing variables called "V" and "SP".

    So e.g. to code for "VALID", if "V" is 1R3, 1R6, 2R3, 2R6, a set of coding method based on "SP" would be implemented, and if "V" is 1L3, 1L6, 2L3, 2L6, another set of coding method based on "SP" would be implemented.

    I have tried to create "VALID" for when "V" is 1R3, 1R6, 2R3, or 2R6.

    If I could have some guidance on where I went wrong with these commands, it would be much appreciated. Thanks all!
    Click image for larger version

Name:	Screen Shot 2019-09-24 at 11.31.57 PM.png
Views:	1
Size:	55.1 KB
ID:	1517760

  • #2
    As for "where I went wrong", you need to tell us

    1. what error messages you got.

    2. what results you got and why they are not what you want

    3. about your data generally. explains more about all those points.

    If V is a string variable then

    gen VALID = cond(V = 1R3, 1R6, 2R3, 2R6)
    should probably be

    gen VALID = inlist(V, "1R3", "1R6", "2R3", "2R6") 
    What you tried would fail at

    V =
    which should be

    V ==
    when you are testing for equality.

    The command

    replace VALID = 0 if else
    is quite wrong. I can't suggest an alternative because I don't know what you want there.


    • #3
      Hi Nick,

      I am very sorry about that.

      1. The error message I got with the commands I showed in the post was r(111) cond not found.
      2. No results were generated
      3. I used all the commands from Stata/SE 16.0. Existing variables "SP" and "V" are both string variables.
      4. The command: replace VALID = 0 if else <-- I am trying to code VALID as "0" when "SP" contains any other strings which are not mentioned in the other specific codes.
      Should it be VALID = 0 else?

      Please do let me know if more clarification is required, thank you so much for your help.


      • #4
        And this is giving me r(198) invalid 'else'
        Attached Files


        • #5
          else is never the last word of a Stata command that I know and certainly not of a replace command. See

          help replace
          for all the allowed syntax.

          Here each variable in Stata is born as 1 if the inlist() call is true and 0 otherwise. So I can't see that you need either of these lines

          I can't copy your commands because you're showing them in an image: see the link in #2 for why you should not do that.

          I think you may just need to delete these lines

          replace valid = 0 else
          As said that is not Stata syntax. Perhaps you are remembering the syntax of some quite different software.


          • #6
            Hi Nick,

            I think I would need a command which specifies that the first 'chunk' of the commands would only be applicable when
            V =="1R3", "1R6", "2R3", "2R6"
            and second 'chunk' only applicable when
            V =="1L3", "1L6", "2L3", "2L6"

            I understand that this 'else' command is still wrong, but I am not quite sure as to what command to state when I want VALID=0 for any other "SP" strings which are not mentioned in the above specified commands?

            gen VALID = inlist(V, "1R3", "1R6", "2R3", "2R6")
            replace VALID = . if SP == "0000"
            replace VALID = 1 if (SP=="2221" | SP=="2231"  | SP=="2211" | SP=="2311" | SP=="2111" | SP=="2223"| SP=="3111")
            replace VALID = 2 if (SP=="2233" | SP=="2331"  | SP=="3311")
            replace VALID = 3 if (SP=="2333" | SP=="3331")
            replace VALID = 0  
            VALID = inlist(V, "1L3", "1L6", "2L3", "2L6")
            replace VALID = . if SP == "0000"
            replace VALID = 1 if (SP=="1112" | SP=="1132"  | SP=="1122" | SP=="1322" | SP=="1222" | SP=="1113"| SP=="3222")
            replace VALID = 2 if (SP=="1133" | SP=="1332"  | SP=="3322")
            replace VALID = 3 if (SP=="1333" | SP=="3332")
            replace VALID = 0

            Thanks a lot for your help.
            Last edited by Sennett Leung; 29 Sep 2019, 07:37.


            • #7
              No; you can't have an

              else { 
              without a preceding if command (an if qualifier is quite different, confusingly or not).

              This may help. There are other ways to do it, but this is among the simplest.

              gen VALID = 0 
              replace VALID == . if SP == "0000"
              gen which = 1 if inlist(V, "1R3", "1R6", "2R3", "2R6")
              replace which = 2 if inlist(V, "1L3", "1L6", "2L3", "2L6") 
              replace VALID = 1 if which == 1 & inlist(SP, "2221", "2231", "2211", "2311", "2111", "2223", "3111")
              replace VALID = 2 if which == 1 & inlist(SP, "2233", "2331", "3311")
              replace VALID = 3 if which == 1 & inlist(SP, "2333", "3331")
              replace VALID = 1 if which == 2 & inlist(SP, "1112", "1132", "1122", "1322", "1222", "1113", "3222")
              replace VALID = 2 if which == 2 & inlist(SP, "1133", "1332", "3322")
              replace VALID = 3 if which == 2 & inlist(SP, "1333", "3332")


              • #8
                Hi Nick,

                Thank you so much for your help! It worked perfectly!

                Many thanks,