Variable that double counts the observations

I am trying to create a new variable such that it would count like 1,1,2,2,3,3,4,4 ..... meaning it would double count the observations. My current code is like this

gen newid = _n
replace newid = newid[_n+1] if mod(newid2,2) == 0

but with this the result comes out as 1,1,3,3,5,5,7,7, ... where the increments are in 2's, i.e. I only get odd numbers. How should I modify this code?

Upvotes: 1

Views: 114

Answers (2)

ander2ed
ander2ed

Reputation: 1338

You might try dividing your ID variable by 2, and then use Stata's ceil function to force it up to the nearest integer.

clear 
set obs 50
gen newid = _n
gen newid2 = ceil(newid/2)

Upvotes: 2

Dinesh Chitlangia
Dinesh Chitlangia

Reputation: 590

You can use the int(x) function. This function returns the integer obtained by truncating x.

Thus, int(5.2) is 5.

If you want the following pattern

    1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

the command is

    gen seq = int((_n-1)/2) +1

Upvotes: 0

Related Questions