and now we will learn how to convert a continuous variable and make it into a discrete variable
usually when we do this we're changing it into ordinal discrete variables and this process is
also sometimes called collapsing a variable and you'll see why visits is a variable in our data
set and it's in the same data set we've been using for this entire video series the numbers
range between 1 and 20 but we would like to break visits down into three categories we would like to
have any value in the variable visitors that are between one and seven inclusively to be assigned
the value of one and to be labeled the word rarely which means the respondent rarely visits the pool
we would like the values somewhere from 18 to 15 inclusive to be assigned the value of number two
and given the label sometimes because if they come to the pool between 8 and 15 times a summer that
means they come sometimes and of course if they visit the pool anywhere between 16 and 20 times
a summer we would like to real able or we recode those values to equal the number 3 and then put
on the label Austin which means anyone who has any respondent who had values in the variable visits
between these amounts visits the pool often now that means that when you look at the data sheet
all of the numbers that are between one and seven inclusively will become the number one in the new
variable that we are going to create and all of the numbers that are between eight and 15
inclusively will be changed to the number two in our new variable and of course there are only two
values that fit the description between 16 and 20 but in the new variable we create they will become
the value of 3 now we can use recode or replace exactly as we learned it in prior videos recode
has more simple code but we're going to add some properties to the recode command in this video
let's name the new variable visits x4 frequency meaning we're labeling the frequency of the visit
we should look at the data before we recode so if we take a peek here and we say okay here we
have vivid and we only have six variables but after we recode visits will have an additional
variable called service s and we'll only have values between 1 and 3 and it will correspond
to the values in the original variable so here is the code you type recode just like we did in
an earlier video then the name of the variable in this case visits but look closely at the syntax
of how to do it if you're recoding many values of a continuous variable this code when you use
recode is a little bit different than we saw in the previous videos because we're recording many
values of a continuous variable and it really is very simple the commandery code then the
name of the variable now what's going on in these parentheses well this is something fantastic in
stata you see this slip forward slash here this is not a division symbol what this is telling
Stata is please recode the variable visits so that in the variable visits every value between 1 and
7 will then be assigned the value of 1 and in the same variable every value between five and 15 will
be reassigned the value to and in this variable visit every value between 16 and 20 inclusive
will be reassigned to the number three then of course because we don't want to mess up
the original variable visits we put comma Jen and we create a new variable visit f so visits f will
only have three values but they will correspond to the value than the original variable as I
demonstrate now I typed in the code off camera so that I wouldn't make a mistake and waste anybody's
time but if you would like to pause the video as you type it in you can type it in exactly as you
see it here we code visits parentheses 1 / 7 equal one space parentheses age / 15 equal to space etc
now look what happens when i hit enter well the fact that i did not get an error message is very
encouraging and when I come up here and open this aha notice there is a new variable visit s and
notice how the values correspond to the original value this is the original variable visit one that
means it's between one and seven to that means is between eight and 15 pick a random one this
one is two and the corresponding value in the original variable is between eight and 15 if
you remember from the diagram there were only two values of three four visits f this is between 16
and 19 this is between 16 and night Ashley 16 and 20 those are the only two number three and those
are the only two values between 16 and 20 so it did recode visits to a variable with only three
values representing the three categories of how frequently somebody visits now let's use replace
to do the exact same thing and get the same result to make your data sheet fresh you should quit data
without saving the changes if you already save the changes you could redownload the data file fresh
this way your new data file will not have the new variable we created so we can create it again now
and see that it's the same thing when you use replace you should check the data after each
step that is what really will improve your skill of coding when working with data so here's the
code ok first you generate a new variable called visit s and when you whenever you generate a new
variable as we learned you have to immediately assign it something but in this case you're using
a logical test your thing create this new variable and make it equal to number one if and only if
this variable for the same respondent in the same row is less than or equal to 7 so let's just type
that in now gen visits f equal 1 so we do this gen visit f equal 1 if and only if the variable
visit for the same row and the same respondent is less than or equal to 7 now let's just double
check the code should work now when i hit enter and i did not get an error message let's check
the data immediately aha there is a new variable there and only the values that correspond to the
values and visits less than or equal to 7 are assigned the number one and for now for now all
the other values in this new variable are blank so what do we do well the next line of code should
say replace visits replaced whatever the value in visits F is and make it equal to 2 if and only if
the original variable visits is greater than 7 and also this original variable is less than equal to
15 and again I like to type it in off camera because I make typos because the microphone is
between me and the keyboard but if you like you can pause the video and type in exactly these
characters after you hit enter if you did not get an error message you should check the data aha now
many of those blank values are now changed to the number two and notice the number 2 corresponds to
the original variable visits for any value between 8 and 15 go ahead and pick any number two then go
across the row to the original variable and you will see the original variable value is between 8
and 15 anytime the new variable shows the number two now at this point I'm absolutely sure that
you know what the final line of code will be it's basically the same as the second to last
line of code with the different cut points for the different values and again if you'd like to pause
the video and type in the code that's fine but I have already typed it in when we hit enter and
look at the data one more time you can see that the two final blank values have been changed to
the number three because there are only two values in the original variable that are between 16 and
20 what you should do after each time is run a table on your new variable and what you will
see is that in the new variable eight out of the 20-item are in category 1 10 of the items in the
new variable or 10 of the values are in category two you know between 8 and 15 and only two items
are between 16 and 20 so this will tell you the frequency of the values in the new variable
after you recode and replace now just to finish properly let's put labels on the values here's
the code that we learned in a prior video label define is the command VF label will be the name
of the label and then one rarely two sometimes three often now of course I like to type in the
code off camera so I don't make a typo but you can pause the video and type in the first line of code
necessary to create the label that we're going to put if you push enter and you do not get an error
message you're in good shape and now the last line of code applies the label we just created
to the new variable let's go ahead and type it in and again I like to type it in off camera
so I don't have a problem with my microphone and when i hit enter I notice I did not get an error
message so when I open up the data sheet I can see the labels are applied the best thing you
can do is compare the table with no labels to the table that has labels tab visits f when i
hit enter and hear all the two tables i don't know if i can put them both in the same shot but notice
aight have the value of 1 which means eight people visit rarely 10 has the value of 2 which means 10
people visit sometime and two people have the value of 3 which means two people visit often