Announcement

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

  • Creation of a loop without non-incremented numerical variables

    Dear Statalist Users,

    This is my first time on Statalist, so I apologize in advance if this is not the place to ask my question. I have a very short question to ask. I have to work on a panel of data on graduate subjects with bachelors, masters, etc.

    In order to isolate the public universities (let's call it publicU) from private ones, I tried to implement a code to remove only the public universities. However, when trying to implement a loop, the code does not work:

    forval publicU= 309|328|334|335|339|354 {
    gen new_`publicU'=1 if ts==`publicU'
    replace new_`publicU'=0 if ts~=`publicU'
    }

    ts represents the codes that represent each university individually which interests me.

    What would be a way to loop only on the above values, please?

    Thank you very much in advance for your answer and help. Have a nice day.

    Best regards,

    Michael

  • #2
    So you have a variable ts that contains a numeric code for university, and you have a list of university codes that are public. Now you want to check if a value in ts is in your list (public) or not (private) and store the results in a new variable. You don't use loops for that in Stata. For checking is something is part of a list you can use the inlist() function, see help inlist . A minimal way of creating the variable you want is gen public = inlist(ts, 309, 328, 334, 335, 339, 354), with a bit more bells and whistles:

    Code:
    label define pub_lb 0 "private" 1 "public"
    gen byte public:pub_lb = inlist(ts, 309, 328, 334, 335, 339, 354) if !missing(ts)
    label var pub_lb "type of university"
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hello Maarten,

      Thank you very much for your answer.

      Thank you also for the code and suggestions provided. This is exactly what I needed.

      Best Regards,


      --
      Michael Duarte Gonçalves
      Teaching and Research Assistant
      Geneva School of Business Administration
      (Haute École de Gestion de Genève - HES-SO Genève)
      Rue de la Tambourine 17
      CH-1227 Carouge
      Switzerland

      Comment

      Working...
      X