KatInterview

User-Week Panel Construction and DiD Analysis in SQL

SQL & DataModel answer
MediumModel answerSQL & Datadifference-in-differencespanel-datacausal-inference

Last updated

Build a clean user-week panel dataset for difference-in-differences analysis, handling treatment timing, household spillovers, and staggered adoption.

You are analyzing the impact of a notification feature on user engagement metrics. Given the following database schema, construct a user-week panel dataset suitable for difference-in-differences (DiD) estimation.

Schema:

  • users(user_id, signup_date, country, household_id, device)
  • subscriptions(sub_id, user_id, drug_id, start_date, auto_refill)
  • reminders(user_id, reminder_dt, channel)
  • csat(user_id, event_dt, csat_score)
  • sessions(user_id, session_dt, minutes, purchase_flag)

Requirements:

Your panel must satisfy these criteria:

  1. Treatment definition: Each user's treatment date is the first date they received a reminder (MIN(reminder_dt)). Map this to the Monday of that week as treat_week.

  2. Control group eligibility: Users should be excluded if anyone else in their household_id was treated earlier. This prevents spillover contamination. Users never receiving reminders are valid controls unless a household member was treated.

  3. Time window: Include only weeks within 8 weeks before through 8 weeks after each user's treatment week. Never-treated users should span the full study period (determined by the earliest and latest treatment weeks across all treated users).

  4. Weekly aggregation: For each user-week, compute:

    • csat_mean_wk: average CSAT score (leave as NULL if no surveys that week)
    • minutes_wk: total session minutes (0 if no sessions)
    • purchases_wk: count of purchase events (0 if no sessions)
  5. Panel structure: Output one row per user-week with columns:

    • user_id, week_start (Monday date)
    • treated_post: 1 if this week is on/after the user's treatment week and user was treated; 0 otherwise
    • ever_treated: 1 if user ever receives treatment, 0 otherwise
    • household_id, device
    • csat_mean_wk, minutes_wk, purchases_wk

Part 1: Write SQL to construct this panel.

Part 2: Write a query computing group-level pre/post means for treated and control groups, and calculate the DiD estimate as: (Treated_Post - Treated_Pre) - (Control_Post - Control_Pre).

Part 3: In Python, outline code to perform propensity score matching. Compute propensity scores using logistic regression on baseline covariates (device type, country, pre-treatment usage metrics), then execute 1:1 nearest-neighbor matching with a caliper of 0.05. Report standardized mean differences (SMDs) before and after matching to assess balance.

Assume PostgreSQL syntax. Users who are treated later contribute to the control group until their own treatment week begins.

Hints

Hint 1

Start by identifying each user's first reminder date, then determine household eligibility by checking for earlier-treated household members.

Hint 2

Generate a complete week spine using generate_series, then cross-join with eligible users to create all possible user-week combinations before filtering by the time window.

Hint 3

For the DiD estimate, create indicator variables for treatment group membership and post-period, then aggregate means within each of the four cells of the 2x2 table.