Tutorial 5: CPS Microdata - Cendat

Skip to content

# Tutorial 5: CPS Microdata¶

The Current Population Survey (CPS) supplements provide specialized microdata on topics like tobacco use, voting, and food security.

Goal: Analyze tobacco use patterns across states using multi-year CPS data.

## Setup¶

In [ ]:
Copied!

    import os
    from cendat import CenDatHelper
    from dotenv import load_dotenv

    load_dotenv()
    cdh = CenDatHelper(key=os.getenv("CENSUS_API_KEY"))

    # Get multiple years of CPS Tobacco Use Supplement
    cdh.list_products(years=[2022, 2023], patterns="/cps/tobacco")
    cdh.set_products()

import os from cendat import CenDatHelper from dotenv import load_dotenv load_dotenv() cdh = CenDatHelper(key=os.getenv("CENSUS_API_KEY")) # Get multiple years of CPS Tobacco Use Supplement cdh.list_products(years=[2022, 2023], patterns="/cps/tobacco") cdh.set_products()

## Step 2: Explore and Select Variables¶

In [ ]:
Copied!

    # See available variable groups
    cdh.list_groups()

# See available variable groups cdh.list_groups()
In [ ]:
Copied!

    # Select specific variables
    # PEA1, PEA3: Tobacco use questions
    # PWNRWGT: Person weight
    cdh.set_variables(["PEA1", "PEA3", "PWNRWGT"])
    cdh.set_geos("state", "desc")

# Select specific variables # PEA1, PEA3: Tobacco use questions # PWNRWGT: Person weight cdh.set_variables(["PEA1", "PEA3", "PWNRWGT"]) cdh.set_geos("state", "desc")

## Step 3: Get Data¶

In [ ]:
Copied!

    response = cdh.get_data(within={"state": ["06", "48"]})

response = cdh.get_data(within={"state": ["06", "48"]})

## Step 4: Analyze with Pooled Weights¶

When combining multiple survey years, divide the weights:

In [ ]:
Copied!

    response.tabulate(
        "PEA1",
        "PEA3",
        strat_by="state",
        weight_var="PWNRWGT",
        weight_div=3  # Divide weight for pooled years
    )

response.tabulate( "PEA1", "PEA3", strat_by="state", weight_var="PWNRWGT", weight_div=3 # Divide weight for pooled years )