Hi SAS Forum,
Below code works well but could you please help me to convert this to a code saving do loop like thing.
data want;
set have;
If Time1 = 'CURRENT' or
Time2 = 'CURRENT' or
Time3 = 'CURRENT' or
Time4 = 'CURRENT'or
Time5 = 'CURRENT' or
Time6 = 'CURRENT' or
Time7 = 'CURRENT' or
Time8 = 'CURRENT' or
Time9 = 'CURRENT' or
Time10 = 'CURRENT' or
Time11 = 'CURRENT' or
Time12 = 'CURRENT'
then flag=1;
Else flag=0;
run;
Thanks
Mirisa
Try it this way:
if whichc('CURRENT', of time1-time12) then flag=1;
else flag=0;
Try it this way:
if whichc('CURRENT', of time1-time12) then flag=1;
else flag=0;
data want (drop=i);
set have;
array Time(*) Time1-Time12;
flag=0;
do i = 1 to 12;
if Time(i) = "CURRENT" then do ;
flag=1;
LEAVE;
end;
end;
run;
If you don't want to worry about the number of Time variables or the case of the strings use:
data want;
set have;
array T(*) Time:;
do i = 1 to dim(T) until(flag);
flag = flag or upcase(T{i}) = "CURRENT";
end;
drop i;
run;
data want;
set have;
array x{*} time1-time12;
flag='CURRENT' in x;
run;
Hi Astounding, Reeza, Zaki, PGStats and Ksharp,
Many thanks to veryone of you.
Reponses are great!
Dunga
Opps. Missing something.
data have;
input ID (Time1 Time2 Time3) ( & $10.);
cards;
1234 CURRENT Current Current
5678 Current Current3 Current
3456 Current Past Current
2345 Currernt CURRENrT Current2
;
run;
data want1;
set have;
array x{*} $ time1-time3;
flag=0;
do i=1 to dim(x);
if prxmatch('/^(current|past)$/i',strip(x{i})) then do;flag=1;leave;end;
end;
drop i;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.