Announcement

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

  • Adding multiple variable conditioned on the value of another

    Hello!

    Struggling again with some coding as I am just returning to stata after a (very) long hiatus.
    idpriv activ1 years1 activ2 years2 activ3 years3 total cumulative years in 3001-3008 max years in 3001-3008
    1 3001 4 3004 2 5002 2 6 4
    2 3003 2 1005 3 1002 3 2 2
    3 4006 4 3007 3 3006 1 4 3
    Idpriv = student id
    acitv1-activ27 = student activity (football, band, newspaper, etc)
    years1-years27 = # of years participating in the associated student activity

    For student activities 3001-3008 (music groups) I would like to calculate the...
    1. Cumulative number of years spent in 3001-3008 student activities
    2. The max value for years across 3001-3008
    I shifted the data into long format, but struggled to sum down the column using a conditional statement.

    I attempted to loop over the variable list, years1-years27, but could not identify how to incorporate the conditional statement.

    foreach var of varlist years1-years27 {
    gen ttlyears = `var' if activ==3001 | activ==3002 | etc.



    I also attempted a forval loop as well, but I knew it was wrong as I was typing because it doesn't reference the variable with the values I am conditioning on. I get the complete row total, but I only want the values for when activ1-activ27 is equal to 3001/3008.

    forval `j' = 3001/3008
    egen ttlyears = rowtotal (years*)



    Unfortunately, I was unable to find an answer elsewhere. I was having similar issues extracting the max value. I greatly appreciate any guidance you can offer!

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte idpriv int activ1 byte years1 int activ2 byte years2 int activ3 byte years3
    1 3001 4 3004 2 5002 2
    2 3003 2 1005 3 1002 3
    3 4006 4 3007 3 3006 1
    end
    
    reshape long activ years, i(idpriv)
    by idpriv: egen cum_years_3001_3008 = ///
        total(cond(inrange(activ, 3001, 3008), years, .))
    by idpriv: egen max_years_3001_3008 = ///
        max(cond(inrange(activ, 3001, 3008), years, .))
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thank you! The struggle I have is that I am working on a cold server and I cannot easily copy/paste. I can save the .log and .do files and transfer those off the server (it takes some time to do so), but cannot copy and paste from the cold server window to the statalist window. I will attempt to use -dataex- next time I post.

      Comment


      • #4
        Understood. I sympathize. I have at times been forced to work on partially or completely isolated systems, too. Heavy security measures inevitably reduce productive efficiency. I appreciate your doing your best in difficult circumstances!

        Comment

        Working...
        X