Let’s build off the example in the last post on Use ChatGPT to help understand complex code. Suppose you wrote that complex code. And you’ve worked all day to figure it out and you are… admittedly… proud and spent. You might think “this code took me forever – there’s no way I’m going to forget what I did”. But the experienced hacker in me says:
We forget. And it happens.
The good news? You’re just one ChatGPT conversation away from solid documentation.
(Lawyerly note: that’s assuming, of course, that you can safely share your code with a search engine. Since I work in teaching and learning, my code is far less proprietary than, say, a bank’s.)
So before you close your coding IDE for the day, try asking ChatGPT something like this:
The response from ChatGPT:
/***************************************************************
Purpose:
Collapse person-level CPS microdata to one row per State x Year
and compute weighted labor market rates overall and by subgroups.
Key columns used:
- state_fip, state_name : state identifiers
- yearquarter : calendar quarter (we extract calendar year)
- unemp (0/1) : unemployed indicator
- in_LF (0/1) : in the labor force indicator
- educ_ltd : education group text (e.g., "Some College")
- child_status : child status text (e.g., "Child < 5")
- WTFINL : person weight (used for weighted rates)
Notes:
- (condition=1) returns 1 if true, 0 if false. Multiplying by WTFINL
gives a weighted count for that condition.
- sum( ... ) / sum( ... ) creates a weighted proportion (rate).
****************************************************************/
/******************************************************** By State */
proc sql;
create table hhs.covid_labor_supply as
select
distinct /* (Redundant with GROUP BY below, but harmless) */
state_fip, /* State FIPS code */
state_name, /* State full name */
year(yearquarter) as Year format=9., /* Collapse quarters into calendar year */
/****************************************************************
Labor Force Status | All Women
- Unemployment rate: weighted unemployed / weighted labor force
- LFP rate: weighted labor force / weighted population
****************************************************************/
sum( (unemp=1) * WTFINL )
/ sum( (in_LF=1) * WTFINL ) as UE_Women
label="Unemployment Rate" format=percent9.1,
sum( (in_LF=1) * WTFINL )
/ sum( WTFINL ) as LFP_Women
label="LFP Rate" format=percent9.1,
/****************************************************************
Labor Force Status | By Education
Same formulas as above, but within each education subgroup.
(Multiplying by (educ_ltd="...") zeros out everyone else.)
****************************************************************/
/* ------------------------- Unemployment (by education) ------------------------- */
sum( (educ_ltd="High School Diploma") * (unemp=1) * WTFINL )
/ sum( (educ_ltd="High School Diploma") * (in_LF=1) * WTFINL ) as UE_Women_HS
label="EDUC <= HS" format=percent9.1,
sum( (educ_ltd="Some College") * (unemp=1) * WTFINL )
/ sum( (educ_ltd="Some College") * (in_LF=1) * WTFINL ) as UE_Women_SCollege
label="Some College" format=percent9.1,
sum( (educ_ltd="College +") * (unemp=1) * WTFINL )
/ sum( (educ_ltd="College +") * (in_LF=1) * WTFINL ) as UE_Women_CollegeP
label="College +" format=percent9.1,
/* --------------------------- LFP (by education) ------------------------------- */
sum( (educ_ltd="High School Diploma") * (in_LF=1) * WTFINL )
/ sum( (educ_ltd="High School Diploma") * WTFINL ) as LFP_Women_HS
label="EDUC <= HS" format=percent9.1,
sum( (educ_ltd="Some College") * (in_LF=1) * WTFINL )
/ sum( (educ_ltd="Some College") * WTFINL ) as LFP_Women_SCollege
label="Some College" format=percent9.1,
sum( (educ_ltd="College +") * (in_LF=1) * WTFINL )
/ sum( (educ_ltd="College +") * WTFINL ) as LFP_Women_CollegeP
label="College +" format=percent9.1,
/****************************************************************
Labor Force Status | By Child Status
Same idea, but grouped by child_status categories.
****************************************************************/
/* ------------------------- Unemployment (by child status) --------------------- */
sum( (child_status="No Children") * (unemp=1) * WTFINL )
/ sum( (child_status="No Children") * (in_LF=1) * WTFINL ) as UE_Women_NoKids
label="No Children" format=percent9.1,
sum( (child_status="Older Children") * (unemp=1) * WTFINL )
/ sum( (child_status="Older Children") * (in_LF=1) * WTFINL ) as UE_Women_OlderKids
label="Older Children" format=percent9.1,
sum( (child_status="Child < 5") * (unemp=1) * WTFINL )
/ sum( (child_status="Child < 5") * (in_LF=1) * WTFINL ) as UE_Women_YoungKids
label="Young Children" format=percent9.1,
/* ----------------------------- LFP (by child status) -------------------------- */
sum( (child_status="No Children") * (in_LF=1) * WTFINL )
/ sum( (child_status="No Children") * WTFINL ) as LFP_Women_NoKids
label="No Children" format=percent9.1,
sum( (child_status="Older Children") * (in_LF=1) * WTFINL )
/ sum( (child_status="Older Children") * WTFINL ) as LFP_Women_OlderKids
label="Older Children" format=percent9.1,
sum( (child_status="Child < 5") * (in_LF=1) * WTFINL )
/ sum( (child_status="Child < 5") * WTFINL ) as LFP_Women_YoungKids
label="Young Children" format=percent9.1
from hhs.hhs_otj_raw
/* GROUP BY 1,2,3 means “group by the 1st, 2nd, and 3rd selected columns”
i.e., state_fip, state_name, Year. This collapses to one row per State-Year. */
group by 1,2,3
/* Sort the output the same way (by state_fip, state_name, Year). */
order by 1,2,3
;
quit;
And a few more helpful hints from ChatGPT:
Wow. Just wow.
A solid prompt to ChatGPT makes my code so much easier to understand — for future me and for my collaborators today.
Even better, it got me thinking about a few things I probably wasn’t considering yet. I love that.
And with that, my brain dump on what I’m loving about using ChatGPT as my coding partner comes to a close.
This list reflects where I’m at right now — and I’m sure it’ll grow as I keep exploring new ways to integrate LLMs into my workflow.
In the meantime, I have just one thing to say to ChatGPT:
I’d also love to hear from you — how are you using LLMs to make your coding life easier?
Bonus points if you share thoughts on how LLMs can support those new to SAS, since I’m especially interested in early-career examples for my work in Academic Programs.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.