and now we will learn how to put labels on variable values why use numeric values with
labels instead of just using non numeric values like we have been doing in earlier videos well
that's because we can run more analyses and manage the data values more efficiently if
instead of using non numeric variables we use numeric variables and put on labels we must be
the ones to tell theta what labels go with what values for example we will use the same data set
that has a variable called gender and we will tell stata the value number one will be labeled
male and the value number two will be labeled female when it comes to the variable continent
we will label Asia with the we will label the value number one with the word Asia the value
number two with the word Australia the value of three with Europe the value was for north america
and the value with five South America so what does that mean well when you look at the previous data
set gender & continent were non numeric values and we could not use replace or recode in order
to change them to different values because they were non-numeric however if we change all of the
appearances of mail to the number one and all the appearances of female to the number two then their
numeric values and we can do things like recode and replays to change the variable very quickly
same thing with continent if we change Asia to the value of one and so on we'll be able to do
that now look closely at gender look closely at continent now this is what the data sheet
would look like if we changed it again previously would be male female female in observation number
one two and three but over here we change male to number one and female to number two so now
there are numeric values that correspond with the non numeric values where one is male and two with
female same thing with continent Asia has the value of one so that's why the top one is one
the second one here in North America we assign the value of four so the number four will be labeled
North America when we finish putting on the labels it will go back to looking like the way it looked
but the only difference is we will think we're managing non-numeric characters but the computer
will know that their numbers with labels there are two simple steps to giving a value label to
a variable that means there are two simple lines of code in the first line of code you create the
label and in that very same first line of code tells data what words go with what numbers so
you're assigning words to the numeric values for that label then in the second step for the second
line of code you simply connect the label that you created in the first step with the variable
you would like it to be with so in our first example we're going to give label values to the
variable gender and a moment ago I had said the value number one will have the label male and the
value number two will have the label female so here's the first line of code that I'm about to
type in the words label define tell commands data to create a new label and the word that follows is
the name of the label this is not the name of the variable this is the name of the label and in step
one state it doesn't even know which variable is going to be connected to its just tell stata
you're creating a label and here's the name of the label you're creating then in the third part
of the line tells data what numbers get which words so for whichever variable we place this
label on when that variable has the number one you will see the word mail when that variable has the
number two you will see the word female but even though we type in this line of code stata does not
yet know which variable this will be applied to until the second line of code so let me go ahead
and type in this line so first the commands label define space now the name of the label gender
label ok and then face one face quote male we should actually put it in caps because that's how
we have the words before male quote space to space quote female close quote this is exactly there are
exactly the lines of the syntax for the first line of code that was explained when i hit enter i did
not get an error message but the data sheet did not change because all we did was create the label
we have not yet applied the label to the variable the second line of code is very simple you type
in label values which tells data you're going to apply the label you created to a particular
variable so the command is label values then type in the name of the variable that you're using and
the label that you're putting on so you type in label values then gender cuz that's the variable
that will get the label then gender label which is the label that you're putting so let me go
ahead and type that in label values gender gender label label value space gender Oh scuze me should
really be lowercase label values label value is now what was the other one gender which is
the name of the variable and the name of the label was gender label that's the variable we created in
the previous step hit enter if we did not get an error message we can take a look at the data sheet
and lo and behold all of the number ones show the word mail and all of the number twos and the prea
in the data set show the word female now watch what happens when I click in a particular value
and I type in the number one hit enter it becomes male I type in the number two it shows female well
gee what if i typed in the number three nothing because we gave no label value for three what if
we type in the number for nothing because we gave no values before we have to turn it back to what
it was which I believe was the number two female in order to help you see what values go with what
labels you can run a cable with the values or the labels in fact sometimes you will need to run both
with large data sets that may be the only way to know what values are associated with what numbers
so we all remember from the earlier videos the tab command will give us a table of the variable that
we asked for so we're asking for a table of the variable gender and just like it looked in earlier
videos there are 10 appearances of mail and 10 appearances of female in the variable gender but
the difference this time is if we type it in again tab gender then put a comma space n 0 L
when you put comma space & O L after asking for a table you're telling stata give me the same table
with no labels and when we hit enter you see the number one which represents male still has
10 and the number two that represent female also has 10 so sometimes you have to run a table with
the label to see what they are then run them again without the labels to understand that
the number one is what the value of male is and number two is what the assigned value of female
is and sometimes running tables this way is the only way to find out especially if a data sheet
is really big with lots and lots of numbers and lots of data in order to fully understand this
we're going to give you a second example here are the values in the variable continent that are in
the data sheet now and here are the labels that we're going to assign to those values and after
the second example you will clearly understand the idea of values and labels ok so the first line of
code should be exactly this the command label to find then the name of the label stata doesn't know
what variable it's going to go on yet all it knows it's going to be called see label for continent
label and that and any variable that this label gets assigned to the value of one in that variable
will read the word Asia the value of two and that variable will read the word Australia and so on so
now you can pause this video at this moment and type this into the command line label define is
telling stata to create a label see label follows labels define so stator knows that's the name of
the label now whichever variable you assign this label to when that variable has the value of one
you will see the word Asia when that variable has the value of two you will see the word Australia
and so on so I bring the cursor to the end of the line and i hit enter and as you can see
we've created the label with no error message and now the second line of code assigns the label to
the variable label values which means we're going to take a previously created table and put it on
an existing variable so you type label values the name of the variable and the name of the label so
let's do that label value the name of the variable in this case is con 10 mint and the name of the
label was see label now when we hit enter we get no error message but now when we look at the data
sheet aha where you used to see the number one you see Asia where you previously saw the number
five or four excuse me you see North America where you previously saw the number five you
see South America now watch this when I click here and change this to the number two when hit enter
says australia if i change it to the number three and hit enter it says europe if i change it to
the number 4 and hit enter it says North America if I put five it says South America but if I put
the number six it just the number six because when we created the variable we did not tell theta what
label the value of six should have so that's the reason why let's change it back to one I believe
it was one let's change it back and that shows that the numbers are there but they are behind
the labels so now let's run a table to see how it looks tab then the name of the variable con 10 and
then put that hit enter and now instead of seeing numbers you see the frequency of appearances of
each continent and that's the way it looked in prior videos when they were non numeric values
but now their numeric values with labels so when i type i can put comma n 0 l and hit enter and
if i look at both tables at the same time what I can see is that Asia has the numeric value
of one Australia has the numeric value of 2 and so on so by running a table with the labels and
running a table without the labels I can see what numeric values are associated with which words
regarding that particular variable so now that the variables are numeric with labels we can use the
recode command to change all of the appearances of Asia to the word Australia we simply type in
recode the name of the variable and all of the number one values of that variable will become
number two that means that every time you saw the word Asia you will now see the word Australia and
you can see how handy this would be if we had five thousand rows and sixty columns and we were
not able to change them ourselves if they were non-numeric so I'm going to type in exactly these
words recode Khun tin then to recode the variable in this variable continent everything that was the
number one becomes the number two now before we do that let's look at the tables okay number one is
Asia and number two is Australia that means after weary code instead of having six instances of the
word Asia and four instances of the word Australia will have ten instances of the word Australia and
none of Asia because all of the number one which is Asia has become number two which is Australia
so we hit enter now I'm going to type tab con 10 and hit enter and now you can see there are
ten instances of the word Australia and none of no appearances of the word Asia because all
of the values of one have become number two which means there are no number ones anymore
which means Asia is not in the table number two in Australia and now there are ten instances of that