Announcement

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

  • Producing text from a list

    Dear Colleagues,

    I have found myself in a situation where I would like to produce a line of text based on a list.

    An example of relevant data would be as follows,

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(pracid everhad)
    1 1
    2 1
    4 1
    7 1
    9 1
    end
    where,
    'pracid' is the GP/Primary care practice identifier;
    and 'everhad' is the variable to indicate if the practice ever recorded a patient record of interest.

    From the example above, I would like the result to be

    "1, 2, 4, 7, 9"

    I have tried searching for existing answers in this forum but have not been successful so far. Apologies if such an answer already exists, and if this is a very simple question.

    I would be grateful for any help and guidance.

    Thank you in advance for your time and kind help.

    Regards,

    Kareem

  • #2
    This will work:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(pracid everhad)
    1 1
    2 0
    4 1
    7 1
    9 1
    end
    
    gen result = ""
    replace result = cond(everhad, result[_n-1] + string(pracid) + " ", result[_n-1])
    replace result = trim(result[_N])
    replace result = subinstr(result, " ", ", ", .)
    Note: As you described the problem, the variable everhad was irrelevant. I re-interpreted your question as meaning that you only wanted the result to include those values of pracid where everhad = 1. To illustrate that this code will skip over any pracid where everhad = 0, I modified the input to include one such case.

    Since you actually only want to generate one result text string that summarizes the values of pracid in the entire data set, it isn't actually necessary to create a new variable for this purpose. This can also be done by looping over the observations and building up the result in a local macro. Thus:

    Code:
    local result
    forvalues i = 1/`=_N' {
        local result `result' `=cond(everhad[`i'], string(pracid[`i']), "")'
    }
    local result: subinstr local result " " ", ", all
    display `"`result'"'
    The code for the first method is, to my eye, simpler and a bit more transparent. There are a few differences between them that might arise in extreme conditions. If your real data set is stretching the limits of memory by virtue of having thousands and thousands of variables, there may not be room to store the variable result. On the other hand, if the real data set is very, very large, putting all those pracid values into a local macro might exceed the maximum permissible length of a local macro. Both of these would be unusual situations, but if either of them seems to apply, that might guide you in choosing between these approaches.

    Comment


    • #3
      How about:

      Code:
      levelsof pracid if everhad, separate(,)

      Comment


      • #4
        Robert Picard Oh, of course! I completely forgot about the -separate()- option in -levelsof-. Clearly the best way to do this.

        Comment

        Working...
        X