As part of the standardization some codes are provided by user which are not standard to use and these have to be converted in to proper SAS algorthim to avoid case sensitivity without manual intervention. HAVE variables are provided by user and these have to be converted into WANT variables to avoid case sensitivity. In the below code for example DECOD have values COMPLETED in different cases , this has to be standardization.
data have;
length have1 have2 have3 have4 $1000;
have1='if strip(DECOD)="COMPLETED" then COMPL = "Yes"; ' ;
want1='IF UPCASE(STRIP(DECOD))="COMPLETED" then COMPL = "Yes"; ' ;
have2='else if strip(DECOD) in ("Not Completed" "PartIAL") and strip(EXPOSED)="N" then COMPL = "Not applicable";';
want2='ELSE IF UPCASE(STRIP(DECOD)) IN ("NOT COMPLETED" "PARTIAL") AND STRIP(EXPOSED)="N" then COMPL = "Not applicable";';
have3='else if strip(DECOD) in ("NOT completed" " ") and strip(EXPOSED)="y" then COMPL = "Ongoing";';
want3='ELSE IF UPCASE(STRIP(DECOD)) IN ("NOT COMPLETED" " ") AND UPCASE(STRIP(EXPOSED))="Y" then COMPL = "Ongoing";';
have4='else COMPL = "No ("||strip(DECOD)||")";';
want4='else COMPL = "No ("||strip(DECOD)||")";';
run ;
data want;
set have ;
array var_{4} $ have1 have2 have3 have4 ;
do i = 1 to 4 ;
if find(var_{i},"then","i")>0 then do ;
if find(compress(var_{i}),')="')>0 then do;
string1=upcase(substr(var_{i},1,find(var_{i},"then","i")-1));
string2=substr(var_{i},find(var_{i},"then","i"));
substr1=tranwrd(tranwrd(scan(string1,1,'='),'STRIP','UPCASE(STRIP'),')','))');
*temporarily replacing due to above issue;
substr1=tranwrd(substr1,'))))',')))');
substr2=scan(string1,2,'=');
var_{i}=strip(substr1)||' = '||strip(substr2)||' '||strip(string2); end;
end ;end;
run;
From the above code I am splitting the code in to two parts based on the keyword THEN and converting left side completely into upcase and then applying upcase function for variables and converting closed parenthesis into double closed parenthesis. But the issue occurs when the IN operator is present and close parenthesis of IN operator is also getting converted into double )) which is wrong. How to avoid such instance or any other alternative to get the desired result.
The issue is when IN operator is present
ELSE IF UPCASE(STRIP(DECOD)) IN ("NOT COMPLETED" " ")) AND UPCASE(STRIP(EXPOSED)) = "Y" then COMPL = "Ongoing";
... View more