Announcement

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

  • #16
    Ok, in that case I'll state it here.

    Let's import this document as an example:

    import delimited "https://fixturedownload.com/download/nba-2018-EasternStandardTime.csv", clear
    With Nick's suggested code, I get the number of games a team has played only on home/away, I mean, only taking one column into consideration.

    gen long id = _n
    bysort home (id) : gen gamesPlayed = _n - 1
    sort id
    I can't think of a way to ask STATA to look for the amount of times a team has played either home or away, which is, looking in both columns.

    Comment


    • #17
      EDIT: Sorry posted two times
      Last edited by Daniel Wass; 14 Jun 2020, 13:06.

      Comment


      • #18
        If possible, please present a fraction of the data, as recommended in the FAQ. If not possible, a toy example will do fine. It seems the real aim is turning different from what was presented so far, hence the need of a new code. Presenting data/command/output under code delimiters is the best approach. As a kind reminder (also, in the FAQ), the appropriate spelling is Stata.
        Best regards,

        Marcos

        Comment


        • #19
          By a fraction of data you mean the table I get from running dataex?

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte roundnumber str16 date str26 location str22(hometeam awayteam) str9 result long id float gamesPlayed
          1 "16/10/2018 20:00" "TD Garden"                  "Boston Celtics"         "Philadelphia 76ers"     "105 - 87"    1 0
          1 "16/10/2018 22:30" "ORACLE Arena"               "Golden State Warriors"  "Oklahoma City Thunder"  "108 - 100"   2 0
          1 "17/10/2018 19:00" "Spectrum Center"            "Charlotte Hornets"      "Milwaukee Bucks"        "112 - 113"   3 0
          1 "17/10/2018 19:00" "Little Caesars Arena"       "Detroit Pistons"        "Brooklyn Nets"          "103 - 100"   4 0
          1 "17/10/2018 19:00" "Bankers Life Fieldhouse"    "Indiana Pacers"         "Memphis Grizzlies"      "111 - 83"    5 0
          1 "17/10/2018 19:00" "Amway Center"               "Orlando Magic"          "Miami Heat"             "104 - 101"   6 0
          1 "17/10/2018 19:30" "Madison Square Garden"      "New York Knicks"        "Atlanta Hawks"          "126 - 107"   7 0
          1 "17/10/2018 19:30" "Scotiabank Arena"           "Toronto Raptors"        "Cleveland Cavaliers"    "116 - 104"   8 0
          1 "17/10/2018 20:00" "Toyota Center"              "Houston Rockets"        "New Orleans Pelicans"   "112 - 131"   9 0
          1 "17/10/2018 20:30" "AT+ACY-T Center"            "San Antonio Spurs"      "Minnesota Timberwolves" "112 - 108"  10 0
          1 "17/10/2018 22:00" "Golden 1 Center"            "Sacramento Kings"       "Utah Jazz"              "117 - 123"  11 0
          1 "17/10/2018 22:30" "Staples Center"             "LA Clippers"            "Denver Nuggets"         "98 - 107"   12 0
          1 "17/10/2018 22:30" "Talking Stick Resort Arena" "Phoenix Suns"           "Dallas Mavericks"       "121 - 100"  13 0
          1 "18/10/2018 20:00" "Wells Fargo Center"         "Philadelphia 76ers"     "Chicago Bulls"          "127 - 108"  14 0
          1 "18/10/2018 20:00" "Capital One Arena"          "Washington Wizards"     "Miami Heat"             "112 - 113"  15 0
          1 "18/10/2018 22:30" "Moda Center"                "Portland Trail Blazers" "Los Angeles Lakers"     "128 - 119"  16 0
          1 "19/10/2018 19:00" "Amway Center"               "Orlando Magic"          "Charlotte Hornets"      "88 - 120"   17 1
          1 "19/10/2018 19:30" "Barclays Center"            "Brooklyn Nets"          "New York Knicks"        "107 - 105"  18 0
          1 "19/10/2018 20:00" "Scotiabank Arena"           "Toronto Raptors"        "Boston Celtics"         "113 - 101"  19 1
          1 "19/10/2018 20:00" "FedExForum"                 "Memphis Grizzlies"      "Atlanta Hawks"          "131 - 117"  20 0
          1 "19/10/2018 20:00" "Target Center"              "Minnesota Timberwolves" "Cleveland Cavaliers"    "131 - 123"  21 0
          1 "19/10/2018 20:00" "Smoothie King Center"       "New Orleans Pelicans"   "Sacramento Kings"       "149 - 129"  22 0
          1 "19/10/2018 20:30" "Fiserv Forum"               "Milwaukee Bucks"        "Indiana Pacers"         "118 - 101"  23 0
          1 "19/10/2018 22:30" "Vivint Smart Home Arena"    "Utah Jazz"              "Golden State Warriors"  "123 - 124"  24 0
          1 "19/10/2018 22:30" "Staples Center"             "LA Clippers"            "Oklahoma City Thunder"  "108 - 92"   25 1
          1 "20/10/2018 19:00" "Bankers Life Fieldhouse"    "Indiana Pacers"         "Brooklyn Nets"          "132 - 112"  26 1
          1 "20/10/2018 19:00" "Capital One Arena"          "Washington Wizards"     "Toronto Raptors"        "113 - 117"  27 1
          1 "20/10/2018 19:30" "Madison Square Garden"      "New York Knicks"        "Boston Celtics"         "101 - 103"  28 1
          1 "20/10/2018 19:30" "Wells Fargo Center"         "Philadelphia 76ers"     "Orlando Magic"          "116 - 115"  29 1
          1 "20/10/2018 20:00" "AmericanAirlines Arena"     "Miami Heat"             "Charlotte Hornets"      "112 - 113"  30 0
          1 "20/10/2018 20:00" "United Center"              "Chicago Bulls"          "Detroit Pistons"        "116 - 118"  31 0
          1 "20/10/2018 20:30" "American Airlines Center"   "Dallas Mavericks"       "Minnesota Timberwolves" "140 - 136"  32 0
          1 "20/10/2018 21:00" "Pepsi Center"               "Denver Nuggets"         "Phoenix Suns"           "119 - 91"   33 0
          1 "20/10/2018 22:00" "Moda Center"                "Portland Trail Blazers" "San Antonio Spurs"      "121 - 108"  34 1
          1 "20/10/2018 22:30" "Staples Center"             "Los Angeles Lakers"     "Houston Rockets"        "115 - 124"  35 0
          1 "21/10/2018 18:00" "Quicken Loans Arena"        "Cleveland Cavaliers"    "Atlanta Hawks"          "111 - 133"  36 0
          1 "21/10/2018 19:00" "Chesapeake Energy Arena"    "Oklahoma City Thunder"  "Sacramento Kings"       "120 - 131"  37 0
          1 "21/10/2018 20:00" "Pepsi Center"               "Denver Nuggets"         "Golden State Warriors"  "100 - 98"   38 1
          1 "21/10/2018 21:00" "Staples Center"             "LA Clippers"            "Houston Rockets"        "115 - 112"  39 2
          1 "22/10/2018 19:30" "TD Garden"                  "Boston Celtics"         "Orlando Magic"          "90 - 93"    40 1
          1 "22/10/2018 19:30" "Scotiabank Arena"           "Toronto Raptors"        "Charlotte Hornets"      "127 - 106"  41 2
          1 "22/10/2018 20:00" "Fiserv Forum"               "Milwaukee Bucks"        "New York Knicks"        "124 - 113"  42 1
          1 "22/10/2018 20:00" "Target Center"              "Minnesota Timberwolves" "Indiana Pacers"         "101 - 91"   43 1
          1 "22/10/2018 20:30" "American Airlines Center"   "Dallas Mavericks"       "Chicago Bulls"          "115 - 109"  44 1
          1 "22/10/2018 21:00" "Vivint Smart Home Arena"    "Utah Jazz"              "Memphis Grizzlies"      "84 - 92"    45 1
          1 "22/10/2018 22:00" "Moda Center"                "Portland Trail Blazers" "Washington Wizards"     "124 - 125"  46 2
          1 "22/10/2018 22:30" "ORACLE Arena"               "Golden State Warriors"  "Phoenix Suns"           "123 - 103"  47 1
          1 "22/10/2018 22:30" "Staples Center"             "Los Angeles Lakers"     "San Antonio Spurs"      "142 - 143"  48 1
          1 "23/10/2018 19:00" "Little Caesars Arena"       "Detroit Pistons"        "Philadelphia 76ers"     "133 - 132"  49 1
          1 "23/10/2018 20:00" "Smoothie King Center"       "New Orleans Pelicans"   "LA Clippers"            "116 - 109"  50 1
          1 "23/10/2018 21:00" "Pepsi Center"               "Denver Nuggets"         "Sacramento Kings"       "126 - 112"  51 2
          1 "24/10/2018 19:00" "State Farm Arena"           "Atlanta Hawks"          "Dallas Mavericks"       "111 - 104"  52 0
          1 "24/10/2018 19:00" "Quicken Loans Arena"        "Cleveland Cavaliers"    "Brooklyn Nets"          "86 - 102"   53 1
          1 "24/10/2018 19:30" "AmericanAirlines Arena"     "Miami Heat"             "New York Knicks"        "110 - 87"   54 1
          1 "24/10/2018 19:30" "Scotiabank Arena"           "Toronto Raptors"        "Minnesota Timberwolves" "112 - 105"  55 3
          1 "24/10/2018 20:00" "United Center"              "Chicago Bulls"          "Charlotte Hornets"      "112 - 110"  56 1
          1 "24/10/2018 20:00" "Toyota Center"              "Houston Rockets"        "Utah Jazz"              "89 - 100"   57 1
          1 "24/10/2018 20:30" "AT+ACY-T Center"            "San Antonio Spurs"      "Indiana Pacers"         "96 - 116"   58 1
          1 "24/10/2018 21:30" "Fiserv Forum"               "Milwaukee Bucks"        "Philadelphia 76ers"     "123 - 108"  59 2
          1 "24/10/2018 22:00" "Talking Stick Resort Arena" "Phoenix Suns"           "Los Angeles Lakers"     "113 - 131"  60 1
          1 "24/10/2018 22:00" "Golden 1 Center"            "Sacramento Kings"       "Memphis Grizzlies"      "97 - 92"    61 1
          1 "24/10/2018 22:30" "ORACLE Arena"               "Golden State Warriors"  "Washington Wizards"     "144 - 122"  62 2
          1 "25/10/2018 19:00" "Little Caesars Arena"       "Detroit Pistons"        "Cleveland Cavaliers"    "110 - 103"  63 2
          1 "25/10/2018 19:00" "Amway Center"               "Orlando Magic"          "Portland Trail Blazers" "114 - 128"  64 2
          2 "25/10/2018 20:00" "Chesapeake Energy Arena"    "Oklahoma City Thunder"  "Boston Celtics"         "95 - 101"   65 1
          2 "25/10/2018 22:30" "Staples Center"             "Los Angeles Lakers"     "Denver Nuggets"         "121 - 114"  66 2
          2 "26/10/2018 19:00" "Spectrum Center"            "Charlotte Hornets"      "Chicago Bulls"          "135 - 106"  67 1
          2 "26/10/2018 19:30" "Madison Square Garden"      "New York Knicks"        "Golden State Warriors"  "100 - 128"  68 2
          2 "26/10/2018 19:30" "Scotiabank Arena"           "Toronto Raptors"        "Dallas Mavericks"       "116 - 107"  69 4
          2 "26/10/2018 20:00" "Toyota Center"              "Houston Rockets"        "LA Clippers"            "113 - 133"  70 2
          2 "26/10/2018 20:00" "Target Center"              "Minnesota Timberwolves" "Milwaukee Bucks"        "95 - 125"   71 2
          2 "26/10/2018 20:00" "Smoothie King Center"       "New Orleans Pelicans"   "Brooklyn Nets"          "117 - 115"  72 2
          2 "26/10/2018 22:00" "Golden 1 Center"            "Sacramento Kings"       "Washington Wizards"     "116 - 112"  73 2
          2 "27/10/2018 19:00" "Little Caesars Arena"       "Detroit Pistons"        "Boston Celtics"         "89 - 109"   74 3
          2 "27/10/2018 19:00" "Smoothie King Center"       "New Orleans Pelicans"   "Utah Jazz"              "111 - 132"  75 3
          2 "27/10/2018 19:30" "State Farm Arena"           "Atlanta Hawks"          "Chicago Bulls"          "85 - 97"    76 1
          2 "27/10/2018 19:30" "Quicken Loans Arena"        "Cleveland Cavaliers"    "Indiana Pacers"         "107 - 119"  77 2
          2 "27/10/2018 19:30" "Wells Fargo Center"         "Philadelphia 76ers"     "Charlotte Hornets"      "105 - 103"  78 2
          2 "27/10/2018 20:00" "AmericanAirlines Arena"     "Miami Heat"             "Portland Trail Blazers" "120 - 111"  79 2
          2 "27/10/2018 20:00" "FedExForum"                 "Memphis Grizzlies"      "Phoenix Suns"           "117 - 96"   80 1
          2 "27/10/2018 20:30" "Fiserv Forum"               "Milwaukee Bucks"        "Orlando Magic"          "113 - 91"   81 3
          2 "27/10/2018 20:30" "AT+ACY-T Center"            "San Antonio Spurs"      "Los Angeles Lakers"     "110 - 106"  82 2
          2 "28/10/2018 17:00" "Barclays Center"            "Brooklyn Nets"          "Golden State Warriors"  "114 - 120"  83 1
          2 "28/10/2018 19:00" "American Airlines Center"   "Dallas Mavericks"       "Utah Jazz"              "104 - 113"  84 2
          2 "28/10/2018 19:00" "Chesapeake Energy Arena"    "Oklahoma City Thunder"  "Phoenix Suns"           "117 - 110"  85 2
          2 "28/10/2018 21:30" "Staples Center"             "LA Clippers"            "Washington Wizards"     "136 - 104"  86 3
          2 "29/10/2018 19:00" "Bankers Life Fieldhouse"    "Indiana Pacers"         "Portland Trail Blazers" "93 - 103"   87 2
          2 "29/10/2018 19:00" "Wells Fargo Center"         "Philadelphia 76ers"     "Atlanta Hawks"          "113 - 92"   88 3
          2 "29/10/2018 19:30" "AmericanAirlines Arena"     "Miami Heat"             "Sacramento Kings"       "113 - 123"  89 3
          2 "29/10/2018 19:30" "Madison Square Garden"      "New York Knicks"        "Brooklyn Nets"          "115 - 96"   90 3
          2 "29/10/2018 20:00" "United Center"              "Chicago Bulls"          "Golden State Warriors"  "124 - 149"  91 2
          2 "29/10/2018 20:00" "Fiserv Forum"               "Milwaukee Bucks"        "Toronto Raptors"        "124 - 109"  92 4
          2 "29/10/2018 20:00" "Target Center"              "Minnesota Timberwolves" "Los Angeles Lakers"     "124 - 120"  93 3
          2 "29/10/2018 20:30" "AT+ACY-T Center"            "San Antonio Spurs"      "Dallas Mavericks"       "113 - 108"  94 3
          2 "29/10/2018 21:00" "Pepsi Center"               "Denver Nuggets"         "New Orleans Pelicans"   "116 - 111"  95 3
          2 "30/10/2018 19:00" "Spectrum Center"            "Charlotte Hornets"      "Miami Heat"             "125 - 113"  96 2
          2 "30/10/2018 19:00" "Quicken Loans Arena"        "Cleveland Cavaliers"    "Atlanta Hawks"          "136 - 114"  97 3
          2 "30/10/2018 19:00" "Amway Center"               "Orlando Magic"          "Sacramento Kings"       "99 - 107"   98 3
          2 "30/10/2018 19:30" "TD Garden"                  "Boston Celtics"         "Detroit Pistons"        "108 - 105"  99 2
          2 "30/10/2018 19:30" "Scotiabank Arena"           "Toronto Raptors"        "Philadelphia 76ers"     "129 - 112" 100 5
          end

          Comment


          • #20
            You want one count variable over two variables. But how do you want that variable to represent two teams at the same time? Just trying to understand better what you want.

            Maybe you can give an example with a small part of the data and what you want your variable to look like given the data.

            Comment


            • #21
              My goal is that the count variable represents the number of games a team has played.

              Nick's suggested code is fine for how many games has the home team played at home, but misses out the games that the home team has played away.

              The example of what I want would be:

              TEAM A TEAM B GAMES PLAYED BY HOME TEAM
              Boston Chicago 0
              Boston Orlando 1
              Chicago Orlando 1 (as Chicago has played one game up to this day)
              Detroit Boston 0 (as Detroit hasn't played before)
              Denver Boston 0 (as Denver hasn't played before)
              Boston Detroit 4 (as Boston has played 4 times before this day)

              An example of what I'm getting would be:

              TEAM A TEAM B GAMES PLAYED BY HOME TEAM
              Boston Chicago 0
              Boston Orlando 1
              Chicago Orlando 0
              Detroit Boston 0
              Denver Boston 0
              Boston Detroit 2

              Thanks for your replies, hope I've explained myself

              Comment


              • #22
                Thanks for the example. Here's one way to do it:
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str7(home away) byte count
                "Boston"  "Chicago" 0
                "Boston"  "Orlando" 1
                "Chicago" "Orlando" 1
                "Detroit" "Boston"  0
                "Denver"  "Boston"  0
                "Boston"  "Detroit" 4
                end
                
                levelsof home, local(teams)
                gen wanted = .
                tempvar countteam
                gen `countteam' = 0
                foreach team of local teams {
                    replace `countteam' = sum(inlist("`team'", home, away)) - 1
                    replace wanted = `countteam' if home == "`team'"
                }
                
                list home away count wanted, noobs sep(0)
                  +------------------------------------+
                  |    home      away   count   wanted |
                  |------------------------------------|
                  |  Boston   Chicago       0        0 |
                  |  Boston   Orlando       1        1 |
                  | Chicago   Orlando       1        1 |
                  | Detroit    Boston       0        0 |
                  |  Denver    Boston       0        0 |
                  |  Boston   Detroit       4        4 |
                  +------------------------------------+

                Comment


                • #23
                  That seems to work correctly. Thank you very much

                  Comment

                  Working...
                  X