Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Conditional replace how to?

    I'm making the switch from 20+ years of SPSS. so bear with me as I learn.

    I create a new variable using generate. I want the new variable, teach_any to be populated based on 8 other variables. If ANY of the 8 is the number "1" I want my new variable to be a "1", no matter the values of any of the other variables. If any of the variables is a "2" I want my new variable to be a 2, unless it's already a "1" from the prior step, in which case I don't want it overwritten. Finally, for variables where any of the values is a "3" then I want to write a 3 to my new variable, unless it is already a 1 or 2.

    Here's my incorrect approach here:

    replace teach_any=1 if class_2==1
    replace teach_any=2 if class_2==2
    replace teach_any=3 if class_2==3

    Step 1 will work, the other two overwrite.

    for step 2, I want it to doif there is not a 1 value in the new variable, how would I accomplish this?

  • #2
    Just for teaching purposes, you could do
    Code:
    gen teach_any=1 if class_2==1
    replace teach_any=2 if class_2==2 & missing(teach_any)
    replace teach_any=3 if class_2==3 & missing(teach_any)
    if teach_any depended only on class_2. But you refer to it depending on a group of 8 different variables, and expanding this approach to cover all 8 variable would be tiresome at best and probably rather complicated.

    You don't say what those variables are called, so let me assume that they are called class_1 through class_8 and are located adjacent to each other, with no other intervening variables, in the data set. The key here is that what you describe is equivalent to setting teach_any equal to the minimum value of class_1 through class_8 (at least if that minimum value is 3 or lower). So you can do this whole calculation as:

    Code:
    egen teach_any = rowmin(class_1-class_8)
    replace teach_any = . if teach_any > 3
    In the future, when asking for help with code, it is best to show example data, and in the future, please do that using the -dataex- command. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Making the switch from SPSS to Stata will entail learning to think about data management and analysis in new ways. The Stata approach is rather different. My recommendation to you is that you not work on a project at this time (unless you are under enormous pressure to do so) and instead take a break long enough to read the User's Guide [U] and Getting Started [GS] volumes of the PDF documentation that comes with your Stata implementation. It's a lengthy read, but it will introduce you to the most fundamental commands (like -egen-) that are used repeatedly in routine data management and analysis in Stata. You will also get the flavor of how problems are approached in Stata. The documentation is very clearly written, and includes plenty of worked examples. No doubt you will not remember the syntax details of each of these commands, but by getting this overview, you will in most circumstances recognize what commands might be of use in solving your problem, and you can then refer to the help files or back to the documentation for the fine points. The time you invest in this reading will rapidly be repaid, and then some, in more efficient use of Stata.

    Comment


    • #3
      Thank you, that did the trick. I'm working through the manual as we speak.

      Comment

      Working...
      X