SAS中文论坛

标题: Help about creating a dummy [打印本页]

作者: shiyiming    时间: 2011-7-24 10:34
标题: Help about creating a dummy
I have a question on creating a dummy if it is satisfied with a certain condition.
Here is the data I have:

firm    year        x1
A    1985        0
A    1986        0
A    1987        0
A    1988        0
A    1989        1
A    1990        1
A    1991        1
A    1992        1
A    1993        1
A    1994        1
B    1991        1
B    1992        1
B    1993        1
B    1994        1
B    1995        1
B    1996        0
B    1997        0
B    1998        0
B    1999        0
B    2000        0
C    1999        1
C    2000        1
C    2001        1
C    2002        0
C    2003        0
C    2004        0
C    2005        0
C    2006        0
D    1977        0
D    1978        0
D    1979        0
D    1980        0
D    1981        0
D    1982        0
D    1983        0
D    1984        0
D    1985        0

I want to create a dummy variable depending on the variable x1.So,  
like A company, the x1 variable starts with 0 and switch to 1 in later years.  I would like to create a dummy equals to 2.
For B company, the x1 variable starts with 1 and switch to 0 in later years.  I would like to have a dummy equals to 1.Again, for C company, the x1 variables starts with 1 and switch to 0 in later years.  I would like to have a dummy equals to 1.
For D company, it never switches.  So I will like the dummy equals to 3.

So the data I want will look like the following:

firm    year        x1        dummy
A    1985        0        2
A    1986        0        2
A    1987        0        2
A    1988        0        2
A    1989        1        2
A    1990        1        2
A    1991        1        2
A    1992        1        2
A    1993        1        2
A    1994        1        2
B    1991        1        1
B    1992        1        1
B    1993        1        1
B    1994        1        1
B    1995        1        1
B    1996        0        1
B    1997        0        1
B    1998        0        1
B    1999        0        1
B    2000        0        1
C    1999        1        1
C    2000        1        1
C    2001        1        1
C    2002        0        1
C    2003        0        1
C    2004        0        1
C    2005        0        1
C    2006        0        1
D    1977        0        3
D    1978        0        3
D    1979        0        3
D    1980        0        3
D    1981        0        3
D    1982        0        3
D    1983        0        3
D    1984        0        3
D    1985        0        3

The years for every company could be different but I do not worry about when the company switch.  But I want to identify it when there is a switch for a given company.

Could anyone help me on this issue?  Thanks in advance!
作者: shiyiming    时间: 2011-7-24 12:16
标题: Re: Help about creating a dummy
[code:27kdq3wr]data temp(keep=firm dummy);
  set yours;
  retain start end;
  by firm year notsorted;
  if first.firm then start=x1;
  if last.firm then do;
     end=x1;
         select(end-start);
         when (0) dummy=3;
         when (1) dummy=2;
         when (-1) dummy=1;
         otherwise;
         end;
         output;
  end;
run;
data wanted;
merge yours temp;
by firm;
run;
[/code:27kdq3wr]
作者: shiyiming    时间: 2011-7-24 12:51
标题: Re: Help about creating a dummy
谢谢
作者: shiyiming    时间: 2011-7-25 10:12
标题: Re: Help about creating a dummy
3-level dummy variable is rare, isn't it?
Jingju




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2