Announcement

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

  • expression for a range of variables

    Hi Friends,
    I have a sample dataset as below. The actual dataset has 58 questions in stead of 10 in the example. I want to generate a new_varaible indicating any of the scale of the question larger than 8. In this example, SubjectNum 006, at VisitNum 2 has one of the question being 9, then the new_variable = "scale large than 8". How should I write the code?

    Thanks.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 SubjectNum byte(VisitNum Q1 Q1a Q2 Q2a Q3 Q3a Q4 Q4a Q5 Q5a)
    "002"  2 1 7 1 7 1 6 1 7 1 8
    "002"  3 0 1 0 1 0 1 0 1 0 1
    "002"  4 1 6 1 7 0 1 1 5 1 6
    "002"  5 0 1 0 1 0 1 0 1 0 1
    "005"  2 1 5 1 5 1 5 0 1 1 6
    "005" 99 . . . . . . . . . .
    "006"  2 1 8 1 9 1 8 1 9 1 9
    "006"  3 1 5 1 5 1 6 1 6 1 5
    "006"  4 1 6 1 6 1 7 1 9 1 8
    "006"  5 1 4 0 1 0 1 0 1 0 1
    end

  • #2
    what is the maximum value that these 58 variables can take? you can probably use the "anymatch" function of the -egen- command; see
    Code:
    help egen

    Comment


    • #3
      You are right! I did not think about that! But If I want to write a code like this. Why it does not work?

      Code:
       
      . foreach var of varlist Q* {
      gen scale_comments = "scale is larger than 8" if `var' > 8
      }

      Comment


      • #4
        Presumably Stata presented you with an error message telling you why it did not work. In that case, if you need help understanding the error message, you'll have to tell us what the error message is. I can see at least one problem, but I can't know if the program even got far enough to experience that problem.

        Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

        Section 12.1 is particularly pertinent

        12.1 What to say about your commands and your problem

        Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
        ...
        Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

        Comment


        • #5
          Rich and William give excellent advice, but despite the lack of an error report, the error can be guessed: Second time around the loop in #3, the variable already exists.

          In any case, having one variable with that content is not informative. I could suggest code for what you ask but is it really going to do what you want? It will end up populated if any of the variables qualifies, but then what will you do?

          What might be easier is to use egen, rowmax() and then look for maxima of 9 or more.

          Comment


          • #6
            Thank you all!

            Comment

            Working...
            X