Announcement

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

  • Forvalues loop not working for replace

    Hello,
    I am attempting to change season for each observation by sport_code using a loop, but I keep receiving the an ''invalid syntax" error message. Can someone help me identify the issue with my code? I am grateful for your feedback!



    gen season=" "
    forvalues i = 5 7 12 18 21 23 24 26 27 33 34 38 47 54 67 {
    replace season="Fall" if sport_code==`i'
    }





    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 dbn byte sport_code str1 season
    "13K430" 1 ""
    "31R455" 1 ""
    "26Q435" 1 ""
    "29Q243" 1 ""
    "14K478" 1 ""
    "26Q415" 1 ""
    "31R028" 1 ""
    "84K730" 1 ""
    "07X670" 1 ""
    "84Q705" 1 ""
    "25Q525" 1 ""
    "25Q670" 1 ""
    "16K455" 1 ""
    "05M692" 1 ""
    "04M555" 1 ""
    "24Q610" 1 ""
    "84M350" 1 ""
    "12X682" 1 ""
    "02M600" 1 ""
    "19K615" 1 ""
    "13K265" 1 ""
    "23K493" 1 ""
    "02M296" 1 ""
    "10X368" 1 ""
    "02M413" 1 ""
    "06M552" 1 ""
    "26Q495" 1 ""
    "09X231" 1 ""
    "25Q252" 1 ""
    "20K445" 1 ""
    "27Q323" 1 ""
    "19K660" 1 ""
    "10X237" 1 ""
    "21K540" 1 ""
    "07X495" 1 ""
    "02M393" 1 ""
    "28Q440" 1 ""
    "27Q308" 1 ""
    "02M282" 1 ""
    "09X350" 1 ""
    "22K405" 1 ""
    "10X696" 1 ""
    "12X251" 1 ""
    "04M435" 1 ""
    "25Q425" 1 ""
    "10X141" 1 ""
    "05M499" 1 ""
    "08X269" 1 ""
    "02M392" 1 ""
    "25Q460" 1 ""
    "02M260" 1 ""
    "02M298" 1 ""
    "84K712" 1 ""
    "30Q501" 1 ""
    "13K412" 1 ""
    "18K563" 1 ""
    "18K617" 1 ""
    "84K356" 1 ""
    "23K697" 1 ""
    "23K644" 1 ""
    "05M670" 1 ""
    "21K690" 1 ""
    "17K122" 1 ""
    "17K531" 1 ""
    "09X365" 1 ""
    "17K382" 1 ""
    "14K071" 1 ""
    "30Q580" 1 ""
    "17K590" 1 ""
    "11X275" 1 ""
    "10X243" 1 ""
    "02M412" 1 ""
    "07X259" 1 ""
    "31R450" 1 ""
    "22K535" 1 ""
    "04M372" 1 ""
    "16K498" 1 ""
    "11X299" 1 ""
    "32K545" 1 ""
    "09X525" 1 ""
    "02M047" 1 ""
    "06M540" 1 ""
    "02M615" 1 ""
    "20K505" 1 ""
    "02M546" 1 ""
    "06M423" 1 ""
    "15K464" 1 ""
    "13K605" 1 ""
    "28Q328" 1 ""
    "13K483" 1 ""
    "02M580" 1 ""
    "15K497" 1 ""
    "29Q326" 1 ""
    "19K504" 1 ""
    "11X514" 1 ""
    "03M403" 1 ""
    "01M696" 1 ""
    "26Q566" 1 ""
    "12X684" 1 ""
    "02M655" 1 ""
    end
    Last edited by Rhina Torres; 27 Jul 2023, 12:56.

  • #2
    -forval- only works with a range and you don't have a range; on the other hand, -foreach- will work with a numlist

    I see, however, no reason for a loop here as the "inlist" function will work; see
    Code:
    h inlist()

    Comment


    • #3
      forvalues doesn't support arbitrary numlists, only regularly spaced integer sequences.

      You need foreach or a one-liner

      Code:
      gen season = "Fall" if inlist(sport_code, 5. 7,12, 21, 23, 24, 26, 27, 33, 34. 38. 47, 54, 67)

      Comment


      • #4
        Wow! What an efficient function! Thank you so much Rich and Nick!

        Comment

        Working...
        X