Announcement

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

  • Efficient way to loop over string values

    I need to code a new variable 0 or 1 depending on the value of a string variable. The following seems to work:
    gen notcountry=0
    foreach nonc in "AFW" "ARB" "AFE" {
    replace notcountry=1 if (cid3==nonc)
    }

    However, I have several dozen values in addition to AFW, ARB, AFE. I can cut and past them from a Word doc but adding the parens is tedious. Is ther some obvious shortcut I'm missing?
    Thanks as always--this has been a great community for years.

  • #2
    I don't think you should need a loop for this.

    Code:
    gen notcountry=0
    replace notcountry = 1 if inlist(cid3, "AFW", "ARB", "AFE")
    Or even just

    Code:
    gen notcountry = inlist(cid3, "AFW", "ARB", "AFE")
    You should be able to add your dozen or so other values to inlist.

    That doesn't really solve your problem w.r.t. having a ton of values to check for. I don't know what the word file you have looks like, but I would probably just put all of the strings you need to check for into a row of an excel document, then save as a .csv. Then open in a text editor to get a list of comma separated strings that you can paste into your inlist() function. A scalable way to do this might be to store the other values in a text file then read them in programatically. That's probably a bit overly complicated for a one off project though. If there is some kind of pattern in the strings you are looking for, like if you want to indicate each three letter code that starts with 'A' or something similar... that could simplify things. I could be missing something (it happens more than I'd like), but I'm not sure I understand exactly why you need to add parens to the for loop in the first place. Seems like you should be able to list all of your strings space separated in the first line of the foreach loop. Also, looks like nonc should be wrapped in compound quotes in the third line of code in #1.
    Last edited by Daniel Schaefer; 12 Sep 2024, 22:09.

    Comment


    • #3
      I think this might be along the lines of what you want. Check this and adapt as needed. -levelsof- is what you probably need to get all values of those upper case non-countries

      Code:
      clear
      webuse auto
      levelsof make if ustrregexm(make, "Honda"), local(lev) // grab all levels of string variable with quotes
      
      gen `c(obs_t)' honda = 0
      foreach mk of local lev {
          replace honda = 1  if ustrregexm(make,"`mk'")
      }
      
      list make honda in 58/65

      Comment


      • #4
        Originally posted by Daniel Schaefer View Post
        I don't think you should need a loop for this.

        Code:
        gen notcountry=0
        replace notcountry = 1 if inlist(cid3, "AFW", "ARB", "AFE")
        Or even just

        Code:
        gen notcountry = inlist(cid3, "AFW", "ARB", "AFE")
        You should be able to add your dozen or so other values to inlist.

        That doesn't really solve your problem w.r.t. having a ton of values to check for. I don't know what the word file you have looks like, but I would probably just put all of the strings you need to check for into a row of an excel document, then save as a .csv. Then open in a text editor to get a list of comma separated strings that you can paste into your inlist() function. A scalable way to do this might be to store the other values in a text file then read them in programatically. That's probably a bit overly complicated for a one off project though. If there is some kind of pattern in the strings you are looking for, like if you want to indicate each three letter code that starts with 'A' or something similar... that could simplify things. I could be missing something (it happens more than I'd like), but I'm not sure I understand exactly why you need to add parens to the for loop in the first place. Seems like you should be able to list all of your strings space separated in the first line of the foreach loop. Also, looks like nonc should be wrapped in compound quotes in the third line of code in #1.
        Definitely much simpler and elegant.

        Comment


        • #5
          Thanks all. This is such a great community.

          Comment

          Working...
          X