and now we will learn how to change the value of the variables that means changing what's inside
the data field box where the columns and rows intersect if we go over to stata and we open
the same data sheet we've been working on we see that these are the values the values and
brown or non-numeric and the values in black are numeric and we are going to learn in this video
different ways to change them in this video I'm going to show you four different ways to change
the values of the variables first we'll show you how to just change them which means just over type
the characters in the data field for example if I wanted to change anyone who visited the pool ten
times and change that value to the number five thousand I could just click right in that data
field and over type the value with the number five thousand and hit enter I can keep hitting enter so
the cursor goes down to the next instance of the number 10 and over type it with the number 5000
then I can come down here to the hope actually I skipped one of the number Ten's change it to
5,000 just over type it that's all i have to do then i can save the data sheet to make sure that
I don't lose my changes but again all I did was look at the data sheet and find the instance or
occurrence of the number 10 and over type it with the number 5000 now let's see what happens when
we run a table if we tab visit and hit enter you can see that there are three appearances
or three instances of the number 5000 you may also notice that in the results window if you
scroll up a little you can see that state of puts a code that you could have typed in
if you wanted to change it with code rather than typing it in but this particular code is called
the replace command and it will only replace one data field at a time so you can see doing
the over type or using the replace command is very inefficient or even impossible if we had
a data sheet of 5000 observations and 20 or 30 variables it would be impossible for the human
eye to go through everything one by one and change each instance of the number 10 to the number 5000
manually and typing in this code is the same thing as doing it manually so that means that the second
way that we will learn to change the values in the data fields will be using the replace command from
the command line let's use the replace command to change the values back to the number 10 if we take
a look at what was typed in here we can see the first one we changed was well let's take a look
at what the actual syntax on the actual code says the first one says replace which is the command
visits which is the variable that we're changing equal five thousand which is the value that we're
changing it to and when it says into it means in the second row of this particular column so the
field or the little box in the second row of the column or the variable visits will be changed to
5,000 now I'm going to copy this into the command line and I'm going to change it to the number 10
so what this reads is replace visits equal 10 into that means replace what it replace the
second observation in the column of visit change that particular box to the number 10 if we take
a quick look at the data we can see that we were successful in changing the second observation in
the column vivid to 5,000 but after I hit enter this number in this box will change back to the
number 10 so now I'm going to hit enter and it says one real change made look back at the data
and you see it changed the second observation in the variable visits to the number 10 let's
change them all back let's scroll up a little see the second one we did was in row eight so
we replaced in the column visits in the eighth observation we over wrote what was there with
the number 5000 now I'm going to copy that to the command line and say no let's change it back to
the number 10 so after I hit enter you will see in the data sheet the eighth row and the column
visits will be changed back to the number 10 I hit enter stata confirms it's changed back we look at
the data and we can see here's the eighth row it has changed back to the number 10 now we only have
to change the fifth one back to the number 10 and of course that's easy let's go back to the
code that stay to put for us when we originally / typed it manually I'm going to copy it into
the command line and change the fifth row in the column visits that particular box should change
to the number 10 i hit enter then when I open the data sheet I see in the fifth row that has been
changed back to the number 10 now obviously these two ways are very inefficient for reasons that I
mentioned if we had thousands of observations and dozens a variable we could not go through
each box one at a time we need to be able to tell theta take every instance of this particular value
and change it to what we need so in order to do that we will use the recode command for example
in the variable visits we wish to change any observed value from ten to five thousand so even
if we had hundreds of rows and there were dozens appearance dozens of appearances of the number
ten as the value we could change all of them to the number five thousand in one shot now you
may well ask why why would we bother changing the number ten to the number five thousand the answer
is who knows often you must change the numeric value of a variable from one number to another
when you're running analyses for social science and there could be any reason but at least let's
see how the command works before we recode we take a look at the data and we see in the column
of visits there are one two three observations that equal the number ten in row number two row
number five and row number 8 respectively if we use the recode command properly that means when
we come back here after only one line of code all three of these observations should be changed to
the number five thousand the syntax or recode is very simple type the word recode then type
the name of the variable in this case visits then open up a parentheses type the number that you're
changing from then equal then the number you're changing it to and closed parenthesis so this
will tell theta we code or change the value of any variable or any cell in the column of visits
from 10 to the number 5000 after we hit enter it says we have three changes made and when we look
at the data sheet we see sealed row number two row number five and row number eight have been changed
in one shot to 5,000 let's go ahead and change it back let's copy from the results window the
code that we typed taste it into the command line and change it back by saying 2 theta let's change
any instance of the number excuse me any instance of the number five thousand which there are now
three of them please change back to the number 10 and then when we hit enter we come back and
we can see even if there were a hundred even if there were a thousand out of twenty thousand rows
and 1000 of them were the number ten in one shot all thousand would have been changed to the number
five thousand so that's how you use recode and it is very efficient now the other more efficient way
to change the values of the variables would be to use the same replace command then we used up here
in step to accept add a simple logical test that means it would change all the values at once even
if there were hundreds of them and it's very simple the code looks like this you type the
word replace then you type the varial variable that you're replacing and then you type in that
variable in this case replace visits equal 5,000 because that's what you're replacing it with then
hit the space bar then type in your test if visit equal equal 10 now why did we type in two equal
signs on this side but only one equal sign on this side I promise I will explain in a moment but what
this code is showing is we're asking stated to replace every value of 10 for this variable visit
with 5,000 so it's like saying replace visits with the number 5000 if it is the number 10 so
when we hit the enter key and we look back at the data we see that there were three replacements may
45 thousand and now we can type in basically the same code and change it back let me copy don't
copy the little dot here on the end just copy the same code now we're going to fix it by saying
replace every instance of the number 10 with the number 5000 excuse me replace the number 5 replace
all all of the variable visit with the number 10 if and only if that value is already 5,000
hit enter go back and you can see now we changed back all three instances of the number 5000 to
the number 10 so you may we'll ask why did we have only one equal sign over here and two equal signs
over there and that brings us to a general rule in stata please note when commanding stata to do
something like assigning a value to avail variable we only use one equal sign however when doing a
logical test to see or to check if something equals something else that's when we use two
equal signs and this is a reoccurring theme that will be explained more deeply in future videos