BookmarkSubscribeRSS Feed
LanMin
Fluorite | Level 6

Hello,

Below is a small sample of a large data set. I would like to create a dummy variable that is based on changes in Rating –

Dto-plus (t-1)=1 ,  if a firm’s Rating changes from a middle notch in year t-1 to a plus notch in year t and maintains a plus notch (not necessarily the same rating as in year t) in year t+1 to t+2, and set to zero otherwise.

How should I program this?

Thanks!

p.s. rating notch was not a variable in the raw data, I put it there to make it easier for you to read, If I create this variable, is it better to use proc format with a numeric value?

firmid year rating rating notch

1001 1990 AAA middle

1001 1991 AA+ plus

1001 1992 AA+ plus

1001 1993 AA middle

1001 1994 AA+ plus

1002 1990 BBB+ plus

1002 1991 BBB+ plus

1002 1992 A- minus

1002 1993 A+ plus

1002 1994 A middle

1002 1995 A- minus

1003 1990 BBB middle

1003 1991 BBB- minus

1003 1992 BB+ plus

1003 1993 BB+ plus

1003 1994 BB- minus

1003 1995 BB- minus

Lan

3 REPLIES 3
DBailey
Lapis Lazuli | Level 10

How about this..although I didn't see any case where the indicator variable was anything other than 0

DATA HAVE;

INPUT firmid year rating $ notch $;

CARDS;

1001 1990 AAA middle

1001 1991 AA+ plus

1001 1992 AA+ plus

1001 1993 AA middle

1001 1994 AA+ plus

1002 1990 BBB+ plus

1002 1991 BBB+ plus

1002 1992 A- minus

1002 1993 A+ plus

1002 1994 A middle

1002 1995 A- minus

1003 1990 BBB middle

1003 1991 BBB- minus

1003 1992 BB+ plus

1003 1993 BB+ plus

1003 1994 BB- minus

1003 1995 BB- minus

;

RUN;

PROC SQL;

CREATE TABLE WANT AS

SELECT

    Curr.*

    ,case when lastyear.notch='middle' and curr.notch='plus' and year1.notch='plus' and year2.notch='plus' then 1 else 0 end as Indicator_Var

FROM   

    HAVE as curr

    left join have as LastYear

        on curr.firmid=lastyear.firmid

            and curr.year-1=lastyear.year

    left join have as year1

        on curr.firmid=year1.firmid

            and curr.year+1=year1.year

    left join have as year2

        on curr.firmid=year2.firmid

            and curr.year+2=year2.year;

quit;

LanMin
Fluorite | Level 6

Thanks so much!

Your way is so efficient. I thought I have to create lags. are there some books for advanced sas programming you would recommend?

-Lan

DBailey
Lapis Lazuli | Level 10

sorry...my background is SQL, not SAS.  Even after using SAS for 10 years, I still find new SAS ways of doing things that are a surprise.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 888 views
  • 0 likes
  • 2 in conversation