Announcement

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

  • Unable to collapse the data in loop

    Hello everyone,

    I am trying to create a loop so that I can add multiple variables for creating mean_var and median_var. However, this data isn't creating any variables; the collapsed_file is the same as my original data.

    Code:
    use appended_data, clear
    
    // Define variables for loop (Renamed)
    
    local renamed_vars varA varB
    
    // Speaker-Interview Level Averages and Medians
    preserve
    foreach var of local renamed_vars {
          collapse (mean) mean_`var' = `var' ///
                        (median) median_`var' = `var', ///
                         by(interview_id speaker_num)
    }
    tempfile collapsed_file
    save `collapsed_file', replace
    restore
    Thanks in advance!

  • #2
    Cross-posted at https://stackoverflow.com/questions/...eate-variables and answered there.

    Please read the FAQ Advice before posting, as you were prompted to do. https://www.statalist.org/forums/help#crossposting explains that we ask people to tell us about cross-posting. You really should not want to create the impression that you don't care if people duplicate time and effort answering questions in different places.

    Comment


    • #3
      I apologize for not mentioning the cross-post earlier. I appreciate your response on Stack Overflow and thank you for taking the time to help. In the future, I will make sure to mention any cross-posts to avoid duplicating efforts.

      Comment


      • #4
        Please check my latest version on SO. I struggled somewhat to try to follow what you want to do.

        Comment


        • #5
          You also posted at https://www.reddit.com/r/stata/comme...op_and_create/

          -- so my comment was far too late to avoid duplication of effort.

          It is a rule on Reddit that you tell people about cross-posting. It's only a request here. That's for the people on Reddit to deal with if they spot it.

          Last edited by Nick Cox; 16 Jan 2025, 04:37.

          Comment

          Working...
          X