Creating a new variable from several existing ones

Man

New Member
#1
Hello,

I already have ID variable, DATE variable, and now I want to create a new variable that will assign a certain number (say new id) to each firm considering a year(!) observation (NOT a whole period of 2000-2002), and generally will show the following:

For example:

ID_________Date________ new_variable
100 ______ 01.2000______ 1
100 ______ 02.2000______ 1
100 ______ 03.2000______ 1
100 ______ 01.2001______ 2
100 ______ 02.2001______ 2
100 ______ 03.2001______ 2
100 ______ 01.2002______ 3
100 ______ 02.2002______ 3
100 ______ 03.2002______ 3

200 ______ 01.2000______ 4
200 ______ 02.2000______ 4
200 ______ 03.2000______ 4
200 ______ 01.2001______ 5
200 ______ 02.2001______ 5
200 ______ 03.2001______ 5
200 ______ 01.2002______ 6
200 ______ 02.2002______ 6
200 ______ 03.2002______ 6
.......

Kind regards
 
Last edited:
#3
Assuming your date variable is formatted as a Stata date (see -help datetime-), one way is:

Code:
clear all
set more off

*----- exmple data -----

input ///
id dat
1 1
1 2
1 3
1 400
1 401
1 402
1 800
1 801
2 1
2 2
2 3
2 400
2 401
2 402
2 403
2 800
2 801
end

format dat %td

list, sepby(id)

*----- what you want -----

gen year = yofd(dat)

egen newvar = group(id year)

list, sepby(id newvar)