This code worked. However, I have been unable to remove columns from this dataset and I have tried various commands. I would like the following variables: mood1, mood2, mood3, mood4, mood5, mood6, sex, cid, newrace, wt1, employment, education, agegroup, incomegroup, uhf42pov, strata, neighpovgroup4_2000.
Ideally, I would like to export this dataset from columns in R to Excel. I would appreciate any of your help as I am a beginner in R.
You may have to install some packages, particularly the "Hmisc" package but I think this will do what you want. R is case-sensitive and some of your problem may be using mood1 when the actual variable is .MOOD1** and so on.
library(Hmisc)
library(tidyverse)
library(haven)
library(writexl)
dat1 <- read_sas("chs2002_public.sas7bdat")
## Folowing code uses an Hmisc function to give us a character vector.
xx <- Cs(MOOD1, MOOD2, MOOD3, MOOD4, MOOD5, MOOD6, sex, cid, newrace, wt1,
employment, education, agegroup, incomegroup, uhf42pov, strata, neighpovgroup4_2000)
## Select the desired columns
dat2 <- dat1[, xx]
write_xlsx(dat2, "nyc.xlsx")