More efficient:
data T1;
X='COE22'; Y="1"; output;
X='XOE32'; Y="2"; output;
X='XOE21'; Y="1"; output;
X='XOE35'; Y="1"; output;
X='XOE35'; Y="7"; output;
run;
data T2;
X='COE20-COE24, COE35'; Y="1,2,3"; A=1; output;
X='XOE20-XOE24, XOE35'; Y="1-2,4"; A=2; output;
run;
filename TESTS "&wdir\tests.sas";
data _null_;
length STR $80;
set T2;
file TESTS;
do XI=1 to countw(X,",");
if DOXELSE then put 'else ' @;
put 'if ' @;
DOXELSE+1;
XSTR=scan(X, XI, ",");
if index(XSTR,"-") then STR= quote(scan(XSTR,1)) || '<= X <=' || quote(scan(XSTR,2));
else STR= 'X=' || quote(strip(XSTR)) ;
put STR 'then do;' ;
do YI=1 to countw(Y,",");
if DOYELSE then put ' else' @;
put ' if ' @;
DOYELSE+1;
YSTR=scan(Y, YI, ",");
if index(YSTR,"-") then STR= quote(scan(YSTR,1)) || '<= Y <=' || quote(scan(YSTR,2));
else STR= 'Y=' || quote(strip(YSTR)) ;
put STR 'then ICCC=' A ';' ;
end;
put 'end;';
DOYELSE=0;
end;
run;
options source2;
data T3;
set T1;
%include TESTS ;
run;
62 options source2; 63 data T3; 64 set T1; 65 %include TESTS ; NOTE: %INCLUDE (level 1) file TESTS is file X:\SASWORK\_TD23384_NZ8037SPSAS9003_\tests.sas. 66 +if "COE20"<= X <="COE24" then do; 67 + if Y="1" then ICCC=1 ; 68 + else if Y="2" then ICCC=1 ; 69 + else if Y="3" then ICCC=1 ; 70 +end; 71 +else if X="COE35" then do; 72 + if Y="1" then ICCC=1 ; 73 + else if Y="2" then ICCC=1 ; 74 + else if Y="33333333444446" then ICCC=1 ; 75 +end; 76 +else if "XOE20"<= X <="XOE24" then do; 77 + if "1"<= Y <="2" then ICCC=2 ; 78 + else if Y="4" then ICCC=2 ; 79 +end; 80 +else if X="XOE35" then do; 81 + if "1"<= Y <="2" then ICCC=2 ; 82 + else if Y="4" then ICCC=2 ; 83 +end; NOTE: %INCLUDE (level 1) ending. 84 run;
Even more efficient
data T1;
X='COE22'; Y="1"; output;
X='XOE32'; Y="2"; output;
X='XOE21'; Y="1"; output;
X='XOE35'; Y="1"; output;
X='XOE35'; Y="7"; output;
run;
data T2;
X='COE20-COE24, COE35'; Y="1,2,3"; A=1; output;
X='XOE20-XOE24, XOE35'; Y="1-2,4"; A=2; output;
run;
filename TESTS "&wdir\tests.sas";
data _null_;
length STR $80;
set T2;
file TESTS;
do XI=1 to countw(X,",");
if XI=1 then put 'if ' @;
else put 'or ' @;
XSTR=scan(X, XI, ",");
if index(XSTR,"-") then STR= quote(scan(XSTR,1)) || '<= X <=' || quote(scan(XSTR,2));
else STR= 'X=' || quote(strip(XSTR)) ;
put STR @;
end;
put ' then do;' ;
if ^index(Y,'-') then put ' if Y in(' Y ') ' @;
else do YI=1 to countw(Y,",");
if YI=1 then put ' if ' @;
else put ' or ' @;
YSTR=scan(Y, YI, ",");
if index(YSTR,"-") then STR= quote(scan(YSTR,1)) || '<= Y <=' || quote(scan(YSTR,2));
else STR= 'Y=' || put(YSTR,$quote.) ;
put STR @;
end;
put ' then ICCC=' A ';' / 'end;';
run;
options source2;
data T3;
set T1;
%include TESTS ;
run;
61 options source2; 62 data T3; 63 set T1; 64 %include TESTS ; NOTE: %INCLUDE (level 1) file TESTS is file X:\SASWORK\_TD23384_NZ8037SPSAS9003_\tests.sas. 65 +if "COE20"<= X <="COE24" or X="COE35" then do; 66 + if Y in(1,2,3 ) then ICCC=1 ; 67 +end; 68 +if "XOE20"<= X <="XOE24" or X="XOE35" then do; 69 + if "1"<= Y <="2" or Y="4" then ICCC=2 ; 70 +end; NOTE: %INCLUDE (level 1) ending. 71 run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.