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:
ID | Simulated | Date | Error_flag |
1 | 0 | Dec2013 | Error |
1 | 1 | Jan2014 | |
1 | 1 | Feb2014 | |
1 | 1 | Mar2014 | |
1 | 0 | Apr2014 | |
1 | 1 | May2014 | |
1 | 1 | Jun2014 | |
1 | 1 | Jul2014 | |
2 | 0 | Jan2014 | |
2 | 1 | Feb2014 | |
2 | 1 | Mar2014 | |
2 | 0 | Apr2014 | Error |
2 | 1 | May2014 | |
2 | 1 | Jun2014 | |
2 | 0 | Jul2014 | |
2 | 1 | Aug2014 |
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:
ID | Simulated | Date | Error_flag | New_error_flag |
1 | 0 | Dec2013 | Error | Error |
1 | 1 | Jan2014 | Error | |
1 | 1 | Feb2014 | Error | |
1 | 1 | Mar2014 | Error | |
1 | 0 | Apr2014 | ||
1 | 1 | May2014 | ||
1 | 1 | Jun2014 | ||
2 | 0 | Jan2014 | ||
2 | 1 | Feb2014 | ||
2 | 1 | Mar2014 | ||
2 | 0 | Apr2014 | Error | Error |
2 | 1 | May2014 | Error | Error |
2 | 1 | Jun2014 | Error | Error |
2 | 0 | Jul2014 | ||
2 | 1 | Aug2014 |
Can you help me?
Thanks a lot,
Brunosm
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;
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;
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;
Reeza,
That's exacly what I want!
Thanks a lot! 🙂
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.
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.