Announcement

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

  • using for loops over numbered variables

    Hi folks,

    I'm working with a dataset that shows activities for a group of people. There are 15 variables, each called "ContractActivity*" (I'm showing three of these below). I'd like to generate a variable identifying observations for which all of these variables are empty.

    The brute force method would be:

    gen noactivity = (ContractActivity1 == "" & ContractActivity2 == "" & ContractActivity3 == "" ...)

    I'm sure there's an easier/better way to do this in Stata, though. I tried the following loop:
    forvalues i=1/14{
    gen hasactivity = (ContractActivity`i' != "")
    }

    And that errored out early on because I didn't know how to tell stata that I wanted to create one variable, not many.

    Here's the data (first time using dataex, please forgive me if this is wrong...):

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id str80(ContractActivity1 ContractActivity2 ContractActivity3)
     1 ""                              ""                                          ""                   
     2 ""                              ""                                          ""                   
     3 ""                              ""                                          ""                   
     4 ""                              ""                                          ""                   
     5 ""                              ""                                          ""                   
     6 ""                              ""                                          ""                   
     7 "CW Behavioral Health Services" ""                                          ""                   
     8 ""                              ""                                          ""                   
     9 ""                              ""                                          ""                   
    10 "Child Care Referral"           "Direct Educational Instruction Evaluation" "Child Care Referral"
    11 "Child Care Referral"           ""                                          ""                   
    12 "Child Care Referral"           ""                                          ""                   
    13 ""                              ""                                          ""                   
    14 "Child Care Referral"           "Housing Search"                            ""                   
    15 "Child Care Referral"           ""                                          ""                   
    16 "Child Care Referral"           ""                                          ""                   
    17 ""                              ""                                          ""                   
    18 "Child Care Referral"           ""                                          ""                   
    19 ""                              ""                                          ""                   
    20 ""                              ""                                          ""                   
    end

    Any suggestions would be very appreciated!
    Nereida

  • #2
    Code:
    egen wanted= rowmiss(ContractActivity*)
    Whichever has "15" indicates all activities missing.

    Comment


    • #3
      Amazing! Thank you so much! *chef's kiss*

      Comment

      Working...
      X