Sometimes, we would like to input raw data manually from a textbook or homework example. This is easy with Stata's input command. You can use input in the Command window, but it is easier to use Stata's Do-file Editor so that you can correct typing mistakes and save your work. You can type help doedit to learn more about using do-files in Stata.
Let's type doedit to open Stata's Do-file Editor.
. doedit
In the Do-file Editor, type the following lines:
input str12 name |
Ringo |
John |
Paul |
George |
end |
The first line tells Stata that we are going to input data for a string variable called name. The number 12 tells input that we want the string variable to allow up to 12 characters for each observation. The next four lines are the raw data, which include the names Ringo, John, Paul, and George. The word end in the last line tells Stata that we are finished adding data.
You can run the commands by clicking on the icon labeled "Execute (do)" on the far right of the toolbar in the Do-file Editor.
Let's type list to view our data.
. list
name | |
1. | Ringo |
2. | John |
3. | Paul |
4. | George |
Next, let's edit our do-file to add a second variable to the dataset.
First, type clear before the input command to clear Stata's memory.
Second, add birthyear to the input command.
Third, type the year data after each name.
Now click the "Execute (do)" icon to run your do-file.
clear
input str12 name birthyear |
Ringo 1940 |
John 1940 |
Paul 1942 |
George 1943 |
end |
Note that we didn't type anything before the variable birthyear. The input command assumes that a variable is numeric unless you precede the variable name with str#. We could have specified the kind of numeric variable and such as byte or int, but this is not necessary, especially for small example datasets. You can learn more about data types by typing help data types.
Let's return to our Do-file and add one more string variable.
clear
input str12 name birthyear str12 instrument |
Ringo 1940 "drums" |
John 1940 "guitar" |
Paul 1942 "bass guitar" |
George 1943 "guitar" |
end |
Notice that I have added double quotes around the data for the variable instrument. I did this because bass guitar contains a space between the words bass and guitar. You will need to place double quotes around any data that contains spaces. This would be necessary for the variable name if we had included full names that contain spaces.
clear
input str12 name birthyear str12 instrument |
"Ringo Watts" 1940 "drums" |
"John Jagger" 1940 "guitar" |
"Paul Wyman" 1942 "bass guitar" |
"George Richard" 1943 "guitar" |
end |
You can now save your data to a Stata dataset by typing save mydata.
. save mydata file mydata.dta saved
You can watch a demonstration of these commands by clicking on the link to the YouTube video below. You can read more about these commands by clicking on the links to the Stata manual entries below.
Read more in the Stata Data Management Reference Manual; see [D] clear, [D] describe, [D] input, and [D] save.