BookmarkSubscribeRSS Feed
terrifk
Calcite | Level 5
# how does health vary with education
ed_table<-
  meps_2012%>%
  filter(rthlth42>0 & !is.na(ednew))%>%
  srvyr::survey_count(rthlth42,ednew)%>%
  select(-n_se)%>%
  group_by(ednew)%>%
  mutate(prop=prop.table(n))%>%
  pivot_wider(names_from=ednew,values_from=c(n,prop))%>% # make the names "pretty"
  inner_join(srh_labels,by=c("rthlth42"="level"))%>%
  select(label,starts_with("prop"))%>%
  rename_with(~str_replace(.,"prop_[0-9] - ",""))

# use pcs42 and compute the mean
ed_table<-
  ed_table%>%
  bind_rows(
    meps_2012%>%
      filter(pcs42>0 & !is.na(ednew))%>%
      group_by(ednew)%>%
      srvyr::summarize(pcs42=srvyr::survey_mean(pcs42))%>%
      select(-pcs42_se)%>%
      pivot_wider(names_from=ednew,values_from=pcs42)%>%
      rename_with(~str_replace(.,"[0-9] - ",""))%>%
      mutate(label="Mean PCS")
  )

ed_table

ed_table%>%  
  write.csv(file="./output/2.5.7.table1.csv")
age_table<-
  meps_2012%>%
  filter(rthlth42>0 & !is.na(age_bands))%>%
  srvyr::survey_count(rthlth42,age_bands)%>%
  select(-ends_with("se"))%>%
  group_by(age_bands)%>%
  mutate(prop=prop.table(n))%>%
  pivot_wider(names_from=age_bands,values_from=c(n,prop))%>% # make the names "pretty"
  inner_join(srh_labels,by=c("rthlth42"="level"))%>%
  select(label,starts_with("prop"))%>%
  rename_with(~str_replace(.,"prop_",""))

# use pcs42 and compute the mean
age_table<-
  age_table%>%
  bind_rows(
    meps_2012%>%
      filter(pcs42>0 & !is.na(age_bands))%>%
      group_by(age_bands)%>%
      srvyr::summarize(pcs42=srvyr::survey_mean(pcs42))%>%
      select(-pcs42_se)%>%
      pivot_wider(names_from=age_bands,values_from=pcs42)%>%
      rename_with(~str_replace(.,"[0-9] - ",""))%>%
      mutate(label="Mean PCS")
  )

age_table
age_table%>%
  write.csv(file="./output/2.5.7.table2.csv")
# how does health vary with education among 25-64 yo
ed_table2<-
  meps_2012%>%
  filter(rthlth42>0 & !is.na(ednew) & age31x %in% 25:64)%>%
  srvyr::survey_count(rthlth42,ednew)%>%
  select(-n_se)%>%
  group_by(ednew)%>%
  mutate(prop=prop.table(n))%>%
  pivot_wider(names_from=ednew,values_from=c(n,prop))%>% # make the names "pretty"
  inner_join(srh_labels,by=c("rthlth42"="level"))%>%
  select(label,starts_with("prop"))%>%
  rename_with(~str_replace(.,"prop_[0-9] - ",""))

# use pcs42 and compute the mean
ed_table2<-
  ed_table2%>%
  bind_rows(
    meps_2012%>%
      filter(pcs42>0 & !is.na(ednew) & age31x %in% 25:64)%>%
      group_by(ednew)%>%
      srvyr::summarize(pcs42=srvyr::survey_mean(pcs42))%>%
      select(-pcs42_se)%>%
      pivot_wider(names_from=ednew,values_from=pcs42)%>%
      rename_with(~str_replace(.,"[0-9] - ",""))%>%
      mutate(label="Mean PCS")
  )

ed_table2

ed_table2%>%
  write.csv(file="./output/2.5.7.table3.csv")
1 REPLY 1
PaigeMiller
Diamond | Level 26

Perhaps you can explain what the code does, and we could point you to equivalent SAS code.

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1045 views
  • 0 likes
  • 2 in conversation