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
- 2
set obs 100
generate count = _n
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 |
- 1
generate count2 = _n if mod(_n,2) == 0
count2 = _n if mod(_n,2) == 0 |
- 1
generate count3 = round(_n/2, 1) if mod(_n, 2) == 0
count3 = round(_n/2, 1) if mod(_n,2) == 0 |
- 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!
The following commands illustrate how to number by a group in Stata using _n.
- 1
- 2
- 3
generate group = round(_n+2, 5)
sort group
by group: generate subgroup = _n
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.
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.
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.
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):
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:
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. |
Using _N
_N gives a count of the total number of observations being worked with. Type the following into Stata to see how.
- 1
- 2
set obs 100
display _N
display _N
- 3
- 4
- 5
- 6
drop _all
sysuse auto.dta
sort foreign
by foreign: generate foreign_count = _N
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
Thanks, this is great.
ReplyDeleteThanks! This helps me a lot!!
ReplyDeleteawesome, thanks!
ReplyDeleteGreat, thank you!
ReplyDeletehow could we count the group? (in your example?
ReplyDeleteSTATA 17 MP Crack Full Version
ReplyDeleteStata 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
if you wanna get new latest features then click here...
ReplyDeleteStata Crack
CPUID HWMonitor Crack
Resharper Crack
HitPaw Screen Recorder Crack
Evernote Crack
Studio 3T Crack
Resharper Crack
IDM Crack
Download Software for PC & Mac
ReplyDeleteYou 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
Amazing content.. Easy and fun to read.. Thanks for sharing such amazing blog post.
ReplyDeleteResharper Crack
HDClone Crack
Lightworks Pro Crack
IBM SPSS Statistics Crack
MediaHuman YouTube to MP3 Converter Crack
Download Software for PC & Mac
ReplyDeleteYou 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