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! 🙂
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.