Announcement

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

  • automating commands that uses many variables

    Hey,

    I am currently in the middle of a project that involves analysing a time diary, and I wish to create a variable measuring how many times certain activities were performed by all participants.

    I currently have these commands:

    gen no_act = 0
    replace no_act = no_act + 1 if pri1 == 127 | pri1 == 133 | pri1 == 128 | pri1 == 108 | pri1 == 135 | pri1 == 132 | pri1 == 131 | pri1 == 129 | pri1 == 136


    The issue is that each diary interval is contained in a separate variable (from pri1 to pri144). I want to run this command for all 144 variables, but feel as if it it is inefficient to re-write the command 144 times.

    Is there any way I can automate this task, perhaps with python?

    Sorry, am very new to this

    Thanks

  • #2
    Isaac:
    have you already taken a look at the -count- function available from -egen-?
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Isaac:
      have you already taken a look at the -count- function available from -egen-?
      Hi,

      I have tried testing:

      count if pri* == 108

      But I am getting

      pri* invalid name

      Is this the wrong way to use variables with the same suffix?

      Comment


      • #4
        Isaac:
        what about:
        Code:
        egen wanted=count(pri*)
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment

        Working...
        X