BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
user95
Fluorite | Level 6

Yes, here it is, 

 

so far the code worked well but just didnt take under account missing values like the following
 
 
YearIDoccurrence_eventHave_with current codeVar_Want
199214264.0.
199314264.0.
199414264.0.
199514264111
199614264111
     
     
     
199215110.1.
199315110.1.
199415110010
199515110010
199615110010
199715110010
199815110010
199915110010
200015110010
200115110010
200215110010
200315110010
200415110010
200515110010
200615110.1.
200715110010
 
if the data starts with a missing value for the event_occurrence variable, it is set in the var_want to either 0 or 1, and the following results get affected by that .
 
In the second example, because the first two rows of missing values are set to 1 and the  rest of the data of event_occurence is =0, following the logic of the code, the remaining values of want_var are then set to 1. But it is wrong, because an event never occurred for that firm throughout those years. So I am looking for a way to take under consideration the missing values, before they affect the values of the following want_var.
 
Thank you
 
 
novinosrin
Tourmaline | Level 20

Okay Thank you @user95 . A good representative sample always helps and also avoid any assumptions plus going back and forth. The fix is very minor though. Try-

 

data want;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
if not missing(y) then _y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 want_var=Event_occurrence;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)<3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)>3 then want_var=_n<=t(_i)-3;
 end;
 if _y then want_var=ifn(year>=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;
user95
Fluorite | Level 6

Greetings, 

 

 

Thank you , it was helpful, one detail I noticed is that if the last line of data for a certain company's event_occurrence variable is missing the var_want is not =., instead it follows the previous rule of having all consecutive years =1 if the last.event_occurrence year is before the last year of data.

 

Concretely, this is what I get

 

YearIDoccurrence_eventWantHave_with current code
200315110111
200415110111
200515110011
200615110111
200715110011
200815110..1

 

Any suggestions on how to fix it?

novinosrin
Tourmaline | Level 20

Okay @user95   Again as you review your data thoroughly it appears the missings goes as missings and your sample contains a lot of missing values. However all these changes are very minor IF-THENs if you follow the code to make changes. While you asked for one step solution which is fine, I really wonder whether you are able to follow and understand the code to edit/update should you consider maintaining in a production environment. Of course I hope you do. Nonetheless, it's easier to have it split into steps to modify/edit/update if you do not understand the code I am afraid.  I leave that up to you.

 

data want;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
if not missing(y) then _y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 want_var=Event_occurrence;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f and Event_occurrence>. then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)<3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)>3 then want_var=_n<=t(_i)-3;
 end;
 if _y and Event_occurrence>.  then want_var=ifn(year>=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;
user95
Fluorite | Level 6

Thank you, 

 

It worked as intended, I am slowly getting used to working with SAS and I deeply appreciate your time and effort.

novinosrin
Tourmaline | Level 20

Hello @user95  The pleasure is all mine rather ours(including @PaigeMiller ). I get the impression you are speeding in your learning which is a good attitude to have. However take the learning in piecemeal, play with simple approaches, see the brevity in code and execution and then consolidate into one step. Like Sir said, we appreciate your well written question considering you seem  new here.  My hope is while it's fun for us to give solutions and treat SAS programming as a video game, it benefits your learning too. Cheers!

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 20 replies
  • 2077 views
  • 1 like
  • 3 in conversation