G) The use command can load files over the internet We can then load the data we just saved using the use command. We can save the data set we just created to disk by issuing the save command.
Input id female race ses str3 schtype prog read write math science socstĪfter running the above program, we can issue the describe command to get aį) The save command reads stores data as Stata data (.dta) files, and the use command loads Stata data files You can also paste this directly into the Command window The following Stata syntax to the do-file editor and run it. Writing a sequence of commands and running them all at once. We can also use the do-file editor to input data. Import excel using hsbdemo.xlsx, sheet("hsbdemo") firstrow clearĮ) Use input to enter data from the keyboard or a do-file On the import excel command below, we specify the sheet where the data are located with the sheet() option and that the variable names are contained in the first row using the firstrow option. Here is what the file hsbdemo.xlsx looks like. The import excel command was introduce in Stata 12. Infix id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12 using schdat.fixĭ) Use import excel to read in Excel files We will use theīelow we use the infile command, where we specify variable names and the column numbers that their corresponding values inhabit. Notice how we make use of theĬodebook in the infix command below. Here is smallĮxample of this type of data with a codebook. Requires a codebook to specify which column(s) corresponds to which variable. Import delimited gender id race ses schtyp prgtype read write math science socst using hs0.raw, delimiter(tab, collapse) clearĬ) Use infix to read in fixed format filesĪnother data format in which data can be stored is fixed format. This file has no variable names, so we must supply them again: We explicitly tell Stata that the delimiter is a tab in the datafile using the delimiter option, and use the suboption collapse to treat multiple tabs as one delimiter. However, some columns (namely columns 6 and 7) may have 1 or 2 tabs between them - it can be hard to tell by visual inspection. The columns are left-justified, suggesting that the file is tab-delimited. Here is a snapshot of the datafile, hs0.raw. We can use the import delimited command to read text files where the fields are separated by any character, such as spaces or tabs. Import delimited gender id race ses schtyp prgtype read write math science socst using hs0_noname.csv, clear Let’s try to read such a file called hs0_noname.csv. If the first line of the data does not contain the variable names, we must supply the names to the import delimited command.
Comma-separated file without variable names We use the describe command to check if the input was successful.ī2. Here are the Stata commands to read these data. Imagine we have a data file, hs0.csv, located in our current working directory. The command reads the first line of the data file to automatically indentify the character used as the separator (the separator can be explicitly specified with the delimiter option). The command import delimited can read text files in which the fields are separated by any character, such as spaces, commas or tabs. Gender,id,race,ses,schtyp,prgtype,read,write,math,science,socst For example, inĮxcel, we can save data as a comma-separated-values format (.csv) file, which is a text file with fields separated by commas. Our first data will come as a spreadsheet, often managed or created by programs such as Excel. We can also clear memory as we load in another dataset using the clear option on one of the data-loading commands (see below)ī) Use import delimited to read in delimited data from other sourcesī1. Before another dataset can be loaded, we must erase all data from memory using the clear command. In Stata, we can only have one dataset loaded in memory at a time. No dataset can be loaded while another dataset is in memory We use the cd command to change directories and then dir or ls to list the contents of the directory.Ĭd w: /* note: directory and path may differ on your computer */Ī2.
#Insheet stata full#
We can read/write to a directory different from the working directory by specifying a full path name when reading/writing files. We start by changing the working directory, which is the default directory (folder) from which Stata will read files and to which Stata will write files. Store the dataset currently in memory on disk inĬlear the entire dataset and everything elseĢ.0 Demonstration and explanation A) Preparing the workspace Read ASCII (text) data created by a spreadsheetĭescribe contents of data in memory or on disk