Announcement

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

  • loops

    Hi,

    I am trying to look at a range of numbers in a loop but am having difficulty.

    My data looks like this:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 hcpcs
    "84155"
    "80051"
    "80061"
    "82040"
    "82247"
    "82565"
    "83090"
    "83615"
    "84075"
    "84155"
    "84450"
    "84460"
    "84520"
    "G0001"
    "93000"
    end
    I am trying to write a loop that will look at 96400-96499 and 96500-96549.

    I have tried a number of different things including:
    foreach var of varlist hcpcs{
    gen chemo = seq_o if `var' == "964**"

    foreach var of varlist hcpcs{
    gen chemo = seq_o if `var' == "964*"

    foreach var of varlist hcpcs{
    gen chemo = seq_o if `var' == "96400/96499"

    seq_o is a number generated across all my observations from 1 to last person.

    I have tried with and without quotation marks around the variable code. When I list out all the numbers my loop works but it is a lot to list out. Any help is appreciated. Sorry if this has been posted recently or I posted it incorrectly.

    Thanks

  • #2
    First, there is no need to use a loop here: your varlist contains only one variable, so there is only one iteration of the loop.

    The syntax for the kind of selection you want to do is:

    Code:
    gen chemo = seq_o if strmatch(hcpcs, "964*")
    Note: This will set chemo to seq_0 whenever hcpcs is 964 followed by anything. Your example data suggests that the only things following 964 would be two digit sequences from 00 to 99. But if there can be a code 9641, or 964105, or 964AB, then this code will also set chemo = seq_o when hcpcs is one of those as well. So if the data are less constrained than I have assumed, post back with either a clearer rule for what to do, or an example that illustrates the full variability of the hcbcs variable.

    Comment


    • #3
      Clyde, thank you so much. This makes so much sense and works. I appreciate your help.

      Anna

      Comment

      Working...
      X