SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 721|回复: 3
打印 上一主题 下一主题

Help about creating a dummy

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-7-24 10:34:26 | 只看该作者

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!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2011-7-24 12:16:27 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2011-7-24 12:51:45 | 只看该作者

Re: Help about creating a dummy

谢谢
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2011-7-25 10:12:18 | 只看该作者

Re: Help about creating a dummy

3-level dummy variable is rare, isn't it?
Jingju
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-10 03:53 , Processed in 0.067407 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表