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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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;
Reeza
Super User

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;
brunosm
Obsidian | Level 7

Reeza,

 

That's exacly what I want!

 

Thanks a lot! 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 800 views
  • 4 likes
  • 3 in conversation