Announcement

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

  • Help with writing code for data with value labels

    Hi all,
    please consider the following data set:
    input str3 ID byte str9 shock byte str1 evnt

    ID shock evnt
    1. 111 category1 y
    2. 111 category2 y
    3. 111 category3 n
    4. 112 category1 y
    5. 112 category2 n
    6. 112 category3 n
    7. end

    . list

    +------------------------+
    | ID shock evnt |
    |------------------------|
    1. | 111 category1 y |
    2. | 111 category2 y |
    3. | 111 category3 n |
    4. | 112 category1 y |
    5. | 112 category2 n |
    |------------------------|
    6. | 112 category3 n |
    +------------------------+
    I am trying to create a new variable shock1 (occurence of shock of category1) for each ID, such that the data set looks like the following:
    input str3 ID byte str1 shock1

    ID shock1
    1. 111 y
    2. 112 y
    3. end

    . list

    +--------------+
    | ID shock1 |
    |--------------|
    1. | 111 y |
    2. | 112 y |
    +--------------+
    I had started by trying to drop observations for which shock equals category2 and category3 but received error messages. Any help would be greatly appreciated.
    Thank you.

    Also, apologies for the tables not coming out as desired. I have installed dataex before generating the data set, but somehow the tables aren't coming out as they should.

  • #2
    I can't comment on the code you don't show or the ensuing error messages. You are quoting and should be using the # key. Barring some renaming, this seems to be the essence of what you seek.

    Code:
    clear 
    input str3 ID byte str9 shock byte str1 evnt
    ID shock evnt
    111 category1 y
    111 category2 y
    111 category3 n
    112 category1 y
    112 category2 n
    112 category3 n
    end
    
    keep if shock == "category1"

    Comment


    • #3
      Originally posted by Nick Cox View Post
      I can't comment on the code you don't show or the ensuing error messages. You are quoting and should be using the # key. Barring some renaming, this seems to be the essence of what you seek.

      Code:
      clear
      input str3 ID byte str9 shock byte str1 evnt
      ID shock evnt
      111 category1 y
      111 category2 y
      111 category3 n
      112 category1 y
      112 category2 n
      112 category3 n
      end
      
      keep if shock == "category1"
      Thank you for your response. However, there seems to be some problem with this. I am getting a
      Code:
      type mismatch
      r(109)
      when I do
      Code:
      keep if shock == "category1"

      Comment


      • #4
        Originally posted by Nick Cox View Post
        I can't comment on the code you don't show or the ensuing error messages. You are quoting and should be using the # key. Barring some renaming, this seems to be the essence of what you seek.

        Code:
        clear
        input str3 ID byte str9 shock byte str1 evnt
        ID shock evnt
        111 category1 y
        111 category2 y
        111 category3 n
        112 category1 y
        112 category2 n
        112 category3 n
        end
        
        keep if shock == "category1"
        I ran this after converting shock to a string variable and it worked. Thank you.

        Comment

        Working...
        X