giordano
giordano

Reputation: 3152

Stata: list only few number of records with condition

I would like to list only few numbers of records with some conditions. Problem: if I use in 1/4 or _n <= 4 and the first 4 records do not satisfy the condition no records are listed. Here is an example:

clear
input x
1
2
3
4
5
6
end
list if x > 4  & _n <= 3
list in 1/3 if x > 4

Does anybody has an idea how can be solved this problem in one line? Thanks for help.

Upvotes: 1

Views: 600

Answers (1)

dimitriy
dimitriy

Reputation: 9460

Put the following code into a file named slist.ado in directory where Stata can see it (like ~/ado/personal). You can find such directories with the -adopath- command.

program define slist 
version 12.1
syntax [varlist] [if], top(int)

#delimit;
tempvar tag;
gen `tag'=1 `if';
sort `tag';
list `varlist' `if' in 1/`top';

end;

The syntax is slist x if x>4, top(4). The if you don't specify x, it will give you all the variables in your dataset.

Upvotes: 2

Related Questions