1

I have already imported my SAS dataset to R with the haven package. Now I need to extract some of the columns from the data and export them to an excel file.

LuizZ
  • 945
  • 2
  • 11
  • 23
Ary
  • 13
  • 2
  • Where are you having issues specifically? There are several options for exporting to Excel, e.g.: https://stackoverflow.com/questions/19414605/export-data-from-r-to-excel – thelatemail Nov 18 '20 at 21:29
  • 1
    If the data is successfully imported into R and you want to go to Excel, then your question has nothing to do with SAS, unless it has some special structure or class because of how the `haven` package imported it. It it does, and that makes the standard Excel export tools have problems, please make your example reproducible by sharing some sample data with `dput()` and sharing the code you've tried. However, if it is a standard `data.frame`, then the solutions in the FAQ about Excel exports should work. – Gregor Thomas Nov 18 '20 at 21:35
  • If you're not familiar with SAS or R, I would suggest downloading the SAS Universal Viewer which allows you to export the data to a CSV directly which you can then filter in Excel as needed. – Reeza Nov 19 '20 at 02:20

1 Answers1

2

If you already imported the file to R, let's assume that you saved it in a dataframe called df.

Then, you can select the rows you want, and save the results to a new dataframe called df2 with the select command from the tidyverse package. Just put the variables' names you want separeted by commas:

library(tidyverse)

df2 <- df %>% select(var1, var2, var3)

After that, you can export the new dataframe to a .csv file, which you may open in Excel:

write.csv(df2,"C:\\Users\\Ary\\Desktop\\MyData.csv", row.names = FALSE)
LuizZ
  • 945
  • 2
  • 11
  • 23
  • You're welcome. Please accept the answer if it worked for you. You just need to click the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – LuizZ Nov 29 '20 at 03:42