Announcement

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

  • Help with creating and merging new variables from existing variables

    Hi,

    So this is my first time posting and also using Stata. I have spent some time trying to figure it out, so forgive me if it is obvious.

    I have dataset from a Qualtrics vignette survey asking participants about decision making in child welfare.

    The participants got one of 8 randomized vignettes with three variables included in the vignette, which are either - adoption/independent, stable/unstable, 1 placement/4placements.

    When I downloaded the survey from Qualtrics, I have variables in Stata that look like the following

    Vignette_DO_ILUS1P (just for clarification, this Vignette_DO_ILUS1P is independent, unstable, 1 placement).
    Vignette_DO_ILUS4P
    Vignette_DO_ILS1P
    Vignette_DO_ILS4P
    Vignette_DO_AUS1P
    Vignette_DO_AUS4P
    Vignette_DO_AS1P
    Vignette_DO_AS4P


    Each of the variables in Stata just show a 1 or a - to indicate if a participant recieved that particular vignette or not.

    I need to create new variables for adoption, independent, stable, unstable, 1 placment and 4 placements and I have tried the following

    gen unstable=0 if Vignette_DO_ILUS1P ==1

    gen independetlive=0 if Vignette_DO_ILUS1P==1

    gen oneplacement=0 if Vignette_DO_ILUS1P ==1

    gen adopt=1 if Vignette_DO_AS4P==1

    gen stable=1 if Vignette_DO_AS4P==1

    gen fourplacement=1 if Vignette_DO_AS4P==1

    That works well enough to make the six new variables, but then I get stuck.

    My primary problem is that I have three other variables that also contain unstable for instance Vignette_DO_AUS1P and I need them all to be combined into a new variable that contains all the participants that had unstable as part of their vignette. And then I need to do the same for the others, adoption, stable, 1 placement, etc.

    I tried variations of
    gen adopt1 =1 if Vignette_DO_AS1P==1
    gen adopt2=1 if Vignette_DO_AUS4P==1
    gen adopt3=1 if Vignette_DO_AUS1P==1
    gen combinedadopt=1 if adopt==1 + adopt1==1 + adopt2==1 + adopt3==1

    But when I looked at browse combinedadopt, I just got a variable with no data just - hyphens all the way down.

    I appreciate any help and direction you all can provide.



  • #2
    There is almost no purpose for having, in Stata, separate variables for stable and unstable, etc. Rather, the useful way to represent this kind of data is with dichotomous variables such as 0 for stable and 1 for unstable. For the number of placements attribute, having a variable coded 1 or 4 will be suitable. The following code will create those for you:
    Code:
    label define stable_unstable 0 "stable" 1 "unstable"
    gen stable_unstable:stable_unstable = 0
    
    label define adoption_independent 0 "adoption" 1 "independent"
    gen adoption_independent:adoption_independent = 0
    
    gen placements = 1
    
    local vbles Vignette_DO_*US*
    foreach v of local vbles {
        replace stable_unstable = 1 if `v' == "1"
    }
    
    
    local vbles Vignette_DO_IL* {
        replace adoption_independent = 1 if `v' == "1"
    }
    
    local vbles Vignette_DO_4P {
        replace placements = 4 if `v' == "1"
    }
    Note: As you do not present example data, this code is untested and may contain errors. It assumes that the Vignette* variables you have are string variables, as - is not a possible value for a numeric variable.

    In the future, when asking for help with code, always show example data, and do so by using the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Hi Clyde,

      Thank you for the response and for the info on dataex.

      That is interesting about not needing two variables. I will keep that in mind for future projects.

      I hope the data below makes it clearer. In the variable manager window, it just has these variables it listed as a bytes not strings. Sorry for not including it the first time. I am still learning. Perhaps I should not have described it as a hyphen.

      Does the data below make more sense of it?

      Thanks,

      Matthew

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(Vignette_DO_ILUS1P Vignette_DO_ILUS4P Vignette_DO_ILS1P Vignette_DO_ILS4P Vignette_DO_AUS1P Vignette_DO_AUS4P Vignette_DO_AS1P Vignette_DO_AS4P)
      . . . . . . 1 .
      . . . 1 . . . .
      . 1 . . . . . .
      . 1 . . . . . .
      . 1 . . . . . .
      . . . 1 . . . .
      . . . . 1 . . .
      . 1 . . . . . .
      . . . . 1 . . .
      . 1 . . . . . .
      . . . . 1 . . .
      . . 1 . . . . .
      . 1 . . . . . .
      . . . 1 . . . .
      . . . . 1 . . .
      . . . . 1 . . .
      . . . . . 1 . .
      . 1 . . . . . .
      1 . . . . . . .
      . . 1 . . . . .
      . 1 . . . . . .
      . . . . 1 . . .
      . . . 1 . . . .
      . . . . 1 . . .
      . . . . . . . 1
      1 . . . . . . .
      . . . . . . . 1
      . . . 1 . . . .
      . . 1 . . . . .
      1 . . . . . . .
      . . . . . . . 1
      . . . . . . . 1
      . . . . 1 . . .
      . . . . . 1 . .
      . . . . 1 . . .
      . 1 . . . . . .
      1 . . . . . . .
      . . . 1 . . . .
      . . . . . 1 . .
      . 1 . . . . . .
      . . . . . . 1 .
      . . 1 . . . . .
      . . 1 . . . . .
      . . . . . . 1 .
      . . 1 . . . . .
      end

      Last edited by Matthew Trail; 31 Jul 2022, 06:17.

      Comment


      • #4
        OK, thank you for the -dataex- example.

        In most situations, it is poor practice in Stata to code a yes/no variable as 1/missing. The most effective representation of dichotomous (binary) variables in Stata is 1 = yes and 0 = no. While it is possible to code around 1/missing representations, it makes the code messier. So I begin by fixing the data. Then I noticed some syntax errors in the code I posted earlier. I have fixed those, and also modified the code to work with the numeric data presented instead of the strings in #1.

        Code:
        mvencode Vignette_DO_*, mv(0)
        
        label define stable_unstable 0 "stable" 1 "unstable"
        gen stable_unstable:stable_unstable = 0
        
        label define adoption_independent 0 "adoption" 1 "independent"
        gen adoption_independent:adoption_independent = 0
        
        gen placements = 1
        
        foreach v of varlist Vignette_DO_*US* {
            replace stable_unstable = 1 if `v'
        }
        
        
        foreach v of varlist Vignette_DO_IL* {
            replace adoption_independent = 1 if `v'
        }
        
        foreach v of varlist Vignette_DO_*4P {
            replace placements = 4 if `v'
        }

        Comment


        • #5
          Hi Clyde,

          Thanks so much for the code and the help. That worked perfectly.

          Comment

          Working...
          X