Let’s use a file called hsb1 from our web site and use that as an example.
use https://stats.idre.ucla.edu/stat/stata/notes/hsb1 , clear (highschool and beyond (200 cases))
To make this simpler, let's just keep the 25 observations with id less than or equal to 25.
keep if id <= 25 (175 observations deleted)
Now let's say we want to export the variables id gender race read write and science. You can see the data for those variables below.
list id gender race read write scienceid gender race read write science 1. 11 male hispanic 34 46 39 2. 20 male hispanic 60 52 61 3. 12 male hispanic 37 44 39 4. 16 male hispanic 47 31 36 5. 7 male hispanic 57 54 47 6. 21 male hispanic 44 44 50 7. 15 male hispanic 39 39 26 8. 22 male hispanic 42 39 56 9. 9 male hispanic 48 49 44 10. 18 male hispanic 50 33 44 11. 5 male hispanic 47 40 . 12. 14 male hispanic 47 41 42 13. 3 male hispanic 63 65 63 14. 24 male asian 52 62 47 15. 8 female hispanic 39 44 44 16. 1 female hispanic 34 44 39 17. 4 female hispanic 44 50 39 18. 2 female hispanic 39 41 42 19. 19 female hispanic 28 46 44 20. 17 female hispanic 47 57 44 21. 6 female hispanic 47 41 40 22. 10 female hispanic 47 54 53 23. 13 female hispanic 47 46 47 24. 23 female asian 65 65 58 25. 25 female asian 47 44 42
Let's write these variables out to a comma separated file using the outsheet command. After outsheet we specify the names of the variables we want to write (if we omit this, it will write all of the variables). We use the comma option (placed after a , ) to indicate we want a comma separated file (by default it will make a tab separated file).
outsheet id gender race read write science using smauto1.csv , comma
We use the type command to see how the file looks. It looks great.
type smauto1.csvid,gender,race,read,write,science 11,"male","hispanic",34,46,39 20,"male","hispanic",60,52,61 12,"male","hispanic",37,44,39 16,"male","hispanic",47,31,36 7,"male","hispanic",57,54,47 21,"male","hispanic",44,44,50 15,"male","hispanic",39,39,26 22,"male","hispanic",42,39,56 9,"male","hispanic",48,49,44 18,"male","hispanic",50,33,44 5,"male","hispanic",47,40, 14,"male","hispanic",47,41,42 3,"male","hispanic",63,65,63 24,"male","asian",52,62,47 8,"female","hispanic",39,44,44 1,"female","hispanic",34,44,39 4,"female","hispanic",44,50,39 2,"female","hispanic",39,41,42 19,"female","hispanic",28,46,44 17,"female","hispanic",47,57,44 6,"female","hispanic",47,41,40 10,"female","hispanic",47,54,53 13,"female","hispanic",47,46,47 23,"female","asian",65,65,58 25,"female","asian",47,44,42
The variables gender and race are really numeric variables that have value labels. For example, gender is really coded 1 and 2 with 1 representing male and 2 representing female. Perhaps we want the numbers, not the labels, for gender and race. If so, we can use the nolabel option and Stata will output the numeric values, not the labels, as shown below.
outsheet id gender race read write science using smauto2.csv , comma nolabel
We can use the type command below and see that now the numeric values of race and gender are output to the file.
type smauto2.csvid,gender,race,read,write,science 11,1,1,34,46,39 20,1,1,60,52,61 12,1,1,37,44,39 16,1,1,47,31,36 7,1,1,57,54,47 21,1,1,44,44,50 15,1,1,39,39,26 22,1,1,42,39,56 9,1,1,48,49,44 18,1,1,50,33,44 5,1,1,47,40, 14,1,1,47,41,42 3,1,1,63,65,63 24,1,2,52,62,47 8,2,1,39,44,44 1,2,1,34,44,39 4,2,1,44,50,39 2,2,1,39,41,42 19,2,1,28,46,44 17,2,1,47,57,44 6,2,1,47,41,40 10,2,1,47,54,53 13,2,1,47,46,47 23,2,2,65,65,58 25,2,2,47,44,42
You can read this kind of file into any program that knows how to read a comma separated file. For example, Excel or SPSS can read this file. In Excel, you would choose file then open and then for files of type select comma separated file (Excel expects those files to have a .csv extension). You can then click the file and open it in Excel.
You can learn more about this by seeing the Stata help file for outsheet.