I need help with the following. My input dataset is as follows:
If one of the values in the QC column is a FAIL, all of the values in the last column 'Final' should be REPEAT, irrespective of what other values are found in the QC column. Desired output dataset:
Thank you.
The following code does not give expected results as no condition is specified for other qc values.
data exp;
set exp;
if QC = "FAIL" then do;
FINAL= "REPEAT";
end;
run;
CodePudding user response:
You have to process the entire data set to determine if no change is needed.
In this example there is a check if FAIL
occurs in any row and then conditionally changes to REPEAT
data have;
id 1;
input value qc $ @@;
datalines;
. FAIL 1 PASS 0 PASS 1 PASS . FAIL
;
%let repeat_flag = 0;
data _null_;
set have;
where qc = 'FAIL';
call symputx ('repeat_flag',1);
stop;
run;
%if &repeat_flag %then %do;
data have;
set have;
qc = 'REPEAT';
run;
%end;
CodePudding user response:
You need to read the data twice. The first time to figure out if there are any QC failures. The second time to get the records again so you can attach the new variable and write them to the output dataset. The first pass can stop as soon as you find any failure.
data want ;
do while(not eof1);
set have end=eof1;
if qc = 'FAIL' then do;
final='REPEAT';
eof1=1;
end;
end;
do while(not eof2);
set have end=eof2;
output;
end;
stop;
run;