Announcement

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

  • Extract a variable list based on variable format?

    Hi,

    I am trying to loop through datasets to process the date variables the same way (e.g., convert a daily date to "month" to check the frequencies over the course of the year using -tab-). Different datasets have different numbers of/names of date variables, but whether the dataset has 1 or 15 dates, I want it to do the same thing to all of them per dataset. For example
    • Dataset 1 might have just date_of_birth plus a dozen other non-date variables.
    • Dataset 2 might have date_of_birth date_of_death admission_date and discharge_date plus another dozen non-date variables (and unfortunately not always with the same naming convention across all the date variables).
    • But all the dates are formatted as %td.
    Has anyone attempted something similar to what I'm describing? Or does anyone have suggestions to design?
    • I'm imagining we'd have to loop through the full variable list in the open dataset, flag each variable that has format=%td and then store the list of found variables as a macro variable, after which I'd loop through each value in the macro variable to do whatever (e.g., recode date_of_birth to a variable stored as %tm and then -tab- the new monthly date variable and repeat for any additional date vars).
    • I see in the -local- extended functions you can extract attributes for a given variable, but not extract variables based on a particular attribute (e.g., when format is %td). Perhaps this could be used to set a condition for the loop imagined above?
    thanks,

    r


  • #2
    I just checked -ds- and I see that it has a -format- option that matches variables with a particular "format patternlist." This appears to be a way to collect the variable lists you need.

    Comment


    • #3
      See also findname from the Stata Journal.

      Comment


      • #4
        Thanks so much, I'll give these a shot.
        r

        Comment

        Working...
        X