Announcement

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

  • Creating a categorical variable based responses to a Likert scale

    I have data from a survey. I want to create a grouping variable but struggling to write the code. Using the following definition, I want to create a categorical variable (high vs. low group):

    High group: IDs with a score of 4 or 5 on at least four of the five questions (belong1 through belong5) and without a score of 1 or 3 on any question.
    Low group: Everyone else not included in the high group.

    The data is shown below.

    Code:
    clear
    input byte(id belong1 belong2 belong3 belong4 belong5)
     1 4 1 2 1 5
     2 5 4 3 5 1
     3 1 5 2 4 3
     4 2 2 2 3 2
     5 3 2 3 4 2
     6 4 5 4 2 3
     7 3 5 2 2 5
     8 1 4 2 2 4
     9 2 3 1 2 3
    10 5 2 2 2 2
    11 3 2 5 3 2
    12 4 1 4 3 1
    13 5 3 3 4 5
    14 2 3 2 4 3
    15 1 2 1 4 2
    16 5 2 4 4 2
    17 2 2 5 5 2
    18 4 2 5 5 2
    19 5 5 4 1 5
    20 5 5 2 1 5
    21 3 2 1 2 2
    22 3 3 1 5 3
    23 3 3 3 1 3
    24 5 5 2 4 4
    25 2 4 4 2 4
    26 1 3 5 4 5
    27 4 3 1 2 3
    28 1 2 2 3 2
    29 5 4 3 2 1
    30 5 5 5 3 1
    31 5 3 4 2 3
    32 3 4 5 3 4
    33 3 5 4 5 5
    34 4 3 4 5 3
    35 4 4 3 4 4
    end
    Last edited by Al Bothwell; 23 Mar 2023, 19:19.

  • #2
    Originally posted by Al Bothwell View Post
    I want to create a categorical variable (high vs. low group):

    High group: IDs with a score of 4 or 5 on at least four of the five questions (belong1 through belong5) and without a score of 1 or 3 on any question.
    Low group: Everyone else not included in the high group.
    Just run through the logic, step by step.
    Code:
    generate byte fourfive = 0
    generate byte twoORthree = 0
    
    foreach var of varlist belong? {
        quietly replace fourfive = fourfive + 1 if `var' >= 4 & !mi(`var')
        quietly replace twoORthree = twoORthree + 1 if inlist(`var', 2, 3)
    }
    
    generate byte group = fourfive >= 4 & !twoORthree
    
    label define Groups 0 Low 1 High
    label values group Groups
    
    sort group twoORthree fourfive
    list, sepby(group twoORthree fourfive) noobs abbreviate(20)

    Comment


    • #3
      Sorry, that should have been oneORthree.
      Code:
      generate byte fourfive = 0
      generate byte oneORthree = 0
      
      foreach var of varlist belong? {
          quietly replace fourfive = fourfive + 1 if `var' >= 4 & !mi(`var')
          quietly replace oneORthree = oneORthree + 1 if inlist(`var', 1, 3)
      }
      
      generate byte group = fourfive >= 4 & !oneORthree
      
      label define Groups 0 Low 1 High
      label values group Groups
      
      sort group oneORthree fourfive
      list, sepby(group oneORthree fourfive) noobs abbreviate(20)

      Comment


      • #4
        Thank you for the solution!

        Comment

        Working...
        X