BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_user_k
Obsidian | Level 7
data example;
	input firmname $ year revenue;
	cards;
APPLE 1991 102
APPLE 2004 232
APPLE 2005 322
APPLE 2006 231
APPLE 2007 234
APPLE 2009 421
APPLE 2010 231
APPLE 2011 442
APPLE 2012 422
APPLE 2013 212
MSMFT 2000 323
MSMFT 2001 231
MSMFT 2004 232
MSMFT 2005 152
MSMFT 2006 345
MSMFT 2007 232
MSMFT 2009 231
MSMFT 2010 341
MSMFT 2011 325
run;

Hi. I am trying to set two dummy variables for the example shown above, depending on whethter, for each firm ('APPLE' and 'MSMFT'), the 'revenue' information exist 1) in two-years in a row and 2) in three-years in a row, and the output should look like below. Thank you in advance!

 

firmname   year   revenue   two   three
APPLE199110200
APPLE200423200
APPLE200532210
APPLE200623111
APPLE200723411
APPLE200942100
APPLE201023110
APPLE201144211
APPLE201242211
APPLE201321211
MSMFT200032300
MSMFT200123110
MSMFT200423200
MSMFT200515210
MSMFT200634511
MSMFT200723211
MSMFT200923100
MSMFT201034110
MSMFT201132511
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

This looks like it should work.  I can't test it right now, but give it a shot and see what you get.

 

data want;

set have;

by firmname;

retain consecutive_count;

years_passed = dif(year);

if first.firmname then consecutive_count=1;

else do;

   if years_passed=1 then consecutive_count + 1;

   else consecutive_count=1;

end;

if consecutive_count >= 2 then two=1;

else two=0;

if consecutive_count >= 3 then three=1;

else three=0;

run;

View solution in original post

2 REPLIES 2
Astounding
PROC Star

This looks like it should work.  I can't test it right now, but give it a shot and see what you get.

 

data want;

set have;

by firmname;

retain consecutive_count;

years_passed = dif(year);

if first.firmname then consecutive_count=1;

else do;

   if years_passed=1 then consecutive_count + 1;

   else consecutive_count=1;

end;

if consecutive_count >= 2 then two=1;

else two=0;

if consecutive_count >= 3 then three=1;

else three=0;

run;

sas_user_k
Obsidian | Level 7

Thank you! The code your provided works well!

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 765 views
  • 2 likes
  • 2 in conversation