Announcement

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

  • Help with else if

    I've been trying to get an else if function to work but I'm having no luck.


    program shark
    if test == "T" {
    replace test2 = "T"
    }
    else {
    replace test2 = "NOT T"
    }
    end
    If I run the program "shark" test2 will be replaced with "T" unconditionally ie. even if test is not equal to "T". What is going wrong?

    The intention is that if test is equal to T then test 2 in the same row will be set to T. Otherwise test 2 on the same row will be set to NOT T
    Last edited by Simon Sampson; 31 Aug 2016, 14:30.

  • #2
    your first "if" is only being evaluated once and thus you get your result; if test is a variable, then you probably want something like the following instead:
    Code:
    replace test2="T" if test=="T"
    replace test2="NOT T" if test!="T"

    Comment


    • #3
      Unless the example you provided is a simplification of some more elaborate problem, I would not bother using -program- for this. Try something like the following instead.

      Code:
      * Make a small dataset for testing
      clear
      input str5 test
      "R"
      "S"
      "T"
      "t"
      "TT"
      "U"
      "V"
      ""
      .
      end
      
      generate str5 test2 = "" // omit this line if test2 already exists in your file
      replace test2 = "T" if test=="T"
      replace test2 = "NOT T" if test != "T" // ! symbol means NOT
      list
      HTH.
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        Amplifying on the responses in #2 and #3, you are confusing the -if- condition, which applies generically as a clause in most Stata commands, with the -if- command. They are different animals.

        If you want to do something with your data and have it apply only to a subset of the data, then the -if- condition is used.

        If you need to make some block of commands conditional on some state of the program, then the -if- command is used. Also note that when a variable is referenced in an -if- command, unless you specify otherwise, Stata assumes you mean the value of that variable in the first observation.

        For more details, see -help if- and -help ifcmd- in the [U] and [P] reference manuals, respectively.

        Comment


        • #5
          Thanks for the help guys. You are right this was a simplification of a problem which was a simplification itself. I'm heading off to bed now as it's very late and I've been working at this all day already - I'll be having stata nightmares all night! But I may as well let you know what I'm actually trying to do in case you can offer some pointers.

          I have data that contains UK postcodes and from that I was trying to extrapolate what UK Postcode Region, UK county and UK region those postcodes belong to. The plan was to first find the UK Postcode Region per https://en.wikipedia.org/wiki/List_o...United_Kingdom. This is the first one or two elements of the postcode string. Then I intend to simply manually put together some kind of table matching postcode region to county and region. Afterwards I can merge on UK postcode region, bringing in the county and region columns.

          Thanks to your help I managed to output the UK postcode regions correctly using the code below.

          Code:
          program dragon
          replace test3= substr(POST,1,1) if substr(POST,2,1) == "0" | substr(POST,2,1) == "1" | substr(POST,2,1) == "2" | substr(POST,2,1) == "3" | substr(POST,2,1) == "5" | substr(POST,2,1) == "6" | substr(POST,2,1) == "7" | substr(POST,2,1) == "8" | substr(POST,2,1) == "9"
          replace test3= substr(POST,1,2) if substr(POST,2,1) != "0" & substr(POST,2,1) != "1" & substr(POST,2,1) != "2" & substr(POST,2,1) != "3" & substr(POST,2,1) != "5" & substr(POST,2,1) != "6" & substr(POST,2,1) != "7" & substr(POST,2,1) != "8" & substr(POST,2,1) != "9"
          end



          Comment


          • #6
            Good morning! Below are five ways of simplifying the code you present in post #5, presented in increasing order of both brevity and opacity. These are untested, but the objective of each should yield to inspection, except for the fifth one, which relies on familiarity with regular expression notation.

            Code:
            generate byte flag = substr(POST,2,1) == "0" | substr(POST,2,1) == "1" | substr(POST,2,1) == "2" | substr(POST,2,1) == "3" | substr(POST,2,1) == "5" | substr(POST,2,1) == "6" | substr(POST,2,1) == "7" | substr(POST,2,1) == "8" | substr(POST,2,1) == "9"
            
            replace test3= substr(POST,1,1) if flag
            replace test3= substr(POST,1,2) if !flag
            drop flag
            
            replace test3= substr(POST,1,1) if strpos("0123456789",substr(post,2,1))>0
            replace test3= substr(POST,1,2) if strpos("0123456789",substr(post,2,1))==0
            
            generate len = cond(strpos("0123456789",substr(post,2,1))==0,1,2)
            replace test3= substr(POST,1,len)
            drop len
            
            replace test3= substr(POST,1,cond(strpos("0123456789",substr(post,2,1))==0,1,2))
            
            replace test3= regexr(POST,"[^A-Z].*","")

            Comment


            • #7
              Here is yet another solution.

              Code:
              replace test3 = substr(POST, 1, (2 - inrange(substr(POST, 2, 1), "0", "9")))
              Nick Cox (2011) explains the core concept.

              Best
              Daniel


              Cox, N. J. (2011). Stata tip 39: In a list or out? In a range or out? The Stata Journal, 6(4), pp. 593–595

              Comment

              Working...
              X