DATA Step, Macro, Functions and more

Using information in previous observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Using information in previous observations

Hello guys,

 

I have a question for you that I am able to solve in Excel or R, but I'm not seeing how can I do it in SAS.

 

Let's suppose that I have this dataset:

 

IDSimulatedDateError_flag
10Dec2013Error
11Jan2014 
11Feb2014 
11Mar2014 
10Apr2014 
11May2014 
11Jun2014 
11Jul2014 
20Jan2014 
21Feb2014 
21Mar2014 
20Apr2014Error
21May2014 
21Jun2014 
20Jul2014 
21Aug2014 

 

I would like to create a new column, New_error_flag, if the following conditions were verified:

a) for each id, if simulated = 0 and error_flag = "Error", then New_error_flag = "Error" and all the observations with following dates and simulated = 1 also get New_error_flag = "Error"

b) if simulated = 0 and error_flag = "", then all the following observations with posterior date and simulated=1 gets New_error_flag =""

 

Sorry for my english, let me show you the final result:

 

IDSimulatedDateError_flagNew_error_flag
10Dec2013ErrorError
11Jan2014 Error
11Feb2014 Error
11Mar2014 Error
10Apr2014  
11May2014  
11Jun2014  
20Jan2014  
21Feb2014  
21Mar2014  
20Apr2014ErrorError
21May2014ErrorError
21Jun2014ErrorError
20Jul2014  
21Aug2014  

Can you help me?

 

Thanks a lot,

 

Brunosm


Accepted Solutions
Solution
‎03-14-2016 11:36 AM
Super User
Posts: 11,343

Re: Using information in previous observations

I think this should get you started:

data want;
   set have;
   length new_error_flag $ 5;
   retain new_error_flag ' ';
   if simulated=0 and error_flag='Error' then new_error_flag='Error';
   if simulated=0 and error_flag='' then call missing(new_error_flag);
run;

View solution in original post


All Replies
Solution
‎03-14-2016 11:36 AM
Super User
Posts: 11,343

Re: Using information in previous observations

I think this should get you started:

data want;
   set have;
   length new_error_flag $ 5;
   retain new_error_flag ' ';
   if simulated=0 and error_flag='Error' then new_error_flag='Error';
   if simulated=0 and error_flag='' then call missing(new_error_flag);
run;
Super User
Posts: 19,815

Re: Using information in previous observations

Use RETAIN which tells SAS to hold the value across the different rows, until you reset it. 

 

data want;
set have;
retain new_error_flag;

if simulated=0 then new_error_flag=error_flag;

run;

proc print data=want;
run;
Occasional Contributor
Posts: 12

Re: Using information in previous observations

Reeza,

 

That's exacly what I want!

 

Thanks a lot! Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 218 views
  • 4 likes
  • 3 in conversation