Nov 6, 2011

Using _n and _N in Stata

This article is all about using _n and _N in Stata. The functions mod() and round() are also covered at the end for your reference. They are particularly useful when using _n and _N


Using _n

Simple Usage

_n is a system variable. Its value is always the current observation being worked with. This is best illustrated by example:
  1. 1
  2. 2
set obs 100
generate count  = _n

line 1 sets the number of observations to 100.

line 2 uses the generate command to create a variable called 'count1'. The result is a variable in which each observation is equal to its current row. The data looks like this:

count = _n
This is a simple way to number your observations. We can expand on this. What if for some reason we only wanted to number every other row? The following should do the trick.

  1. 1
generate count2 = _n if mod(_n,2) == 0


count2 = _n if mod(_n,2) == 0
By the way, this is one way of selecting alternating rows in Stata. What if we wanted to go a bit further and number each alternating row successively (1, 2, 3, etc.) instead of by twos (2, 4, 6, etc.)? It can be done!
  1. 1
generate count3 = round(_n/2, 1) if mod(_n, 2) == 0

count3 = round(_n/2, 1) if mod(_n,2) == 0
Lets break this down. round(_n/2, 1) rounds the current row number/2 to the nearest integer. if mod(_n,2) == 0 restricts this action to only even numbered rows. So, for the fourth row, the computer does something like this:

  • round(4/2, 1) if mod(4, 2) == 0
  • round(2, 1) if 0 == 0
  • 2 if 0 == 0 (True)
  • 2
Looks like there are some useful things we can do with _n. But there is more!

Numbering by Group

The following commands illustrate how to number by a group in Stata using _n.
  1. 1
  2. 2
  3. 3
generate group = round(_n+2, 5)
sort group
by group: generate subgroup = _n

line 1 - generates a variable which classifies observations into groups of five.

line 2 - group is sorted because it is required for the next command.

line3 - This line generates the variable subgroup by group. by is a prefix command, and it generates the variable subgroup as seen below.

subgroups numbered by group.
Notice that numbering restarts based on group. Using by causes this numbering to occur independently by group.

Using _N

_N gives a count of the total number of observations being worked with. Type the following into Stata to see how.
  1. 1
  2. 2
set obs 100
display _N
The output should be 100. This is an easy way to get see how many observations are in your dataset, but it can also count the number of observations based on a variable which groups observations. To do this, you use the by prefix command. The following example loads up an automotive dataset included with Stata and counts the number of foreign and domestic cars in it.
  1. 3
  2. 4
  3. 5
  4. 6
drop _all
sysuse auto.dta
sort foreign
by foreign: generate foreign_count = _N



line 3 will drop all of the data you have in Stata. If you open up Stata without loading any data you don't need to do this. line 4 loads an example automotive dataset included with Stata.

line 5 sorts foreign. This is required when using the by prefix command.

line 6 counts how many foreign cars there are and domestic cars and assigns these counts to a variable: foreign_count. Each car which is domestic or foreign will have the total number of cars in its respective class stored in this variable (as shown):
Totals are created in every row using this method.



Function Reference
mod(x, y)
mod is short for modulus. This function gives the remainder you would find by dividing x by y. For instance:
  • mod(4,3) = 1
  • mod(3,3) = 0
  • mod(15,6) = 3

round(x,y)
The round function takes x and rounds it to the nearest multiple of y. For example:
  • round(5.97, 1) = 6
  • round(5.88, 0.1) = 5.9
  • round(347,5) = 345

10 comments:

  1. Thanks, this is great.

    ReplyDelete
  2. Thanks! This helps me a lot!!

    ReplyDelete
  3. Great, thank you!

    ReplyDelete
  4. how could we count the group? (in your example?

    ReplyDelete
  5. STATA 17 MP Crack Full Version
    Stata 17.0 MP Crack Full Version is an integrated statistical tool which gives data analysis
    Stata 17 MP Crack Full Version Full Download! Stata 17 MP Crack Full Version is flexible and powerful statical software for the science field
    Link Download Stata 17 MP Crack Full Version
    https://dik.si/MPV17

    ReplyDelete
  6. Download Software for PC & Mac
    You make it look very easy with your presentation, but I think this is important to Be something that I think I would never understand
    It seems very complex and extremely broad to me. I look forward to your next post,
    VoiceMod Pro Crack
    VideoProc Crack
    GameMaker Studio Ultimate Crack
    Zoner Photo Studio Crack
    CleanMyMac Crack
    Evernote Crack
    Revo Uninstaller Pro Crack

    ReplyDelete
  7. Download Software for PC & Mac
    You make it look very easy with your presentation, but I think this is important to Be something that I think I would never understand
    It seems very complex and extremely broad to me. I look forward to your next post,
    Far Cry 5 Crack
    ARCHICAD Crack
    Exiland Backup Professional Crack
    Resharper Crack
    EDIUS Pro Crack
    Minecraft Crack
    Global Mapper Crack

    ReplyDelete