Solved
Contributor
Posts: 39

# Do loop question

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

Accepted Solutions
Solution
‎12-17-2015 11:30 PM
Super User
Posts: 6,751

## Re: Do loop question

Try it this way:

if whichc('CURRENT', of time1-time12) then flag=1;

else flag=0;

All Replies
Solution
‎12-17-2015 11:30 PM
Super User
Posts: 6,751

## Re: Do loop question

Try it this way:

if whichc('CURRENT', of time1-time12) then flag=1;

else flag=0;

Super User
Posts: 23,663

## Re: Do loop question

You don't need a do loop you need the whichc function.

If whichc('Current', of time1-time12)>0 then flag=1;
Super User
Posts: 23,663

## Re: Do loop question

Make sure the case matches...
Super Contributor
Posts: 490

## Re: Do loop question

[ Edited ]
``````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;``````
Posts: 5,521

## Re: Do loop question

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;``````
PG
Regular Contributor
Posts: 244

## Re: Do loop question

Hi PG, Your code working perfect. Would you please show me the code when there are two variable I am comparing with?
I mean flag the observation when:
a) 'Current' AND 'Past' exist and
b) 'Current' OR 'Past' exist.
(using upcase in both cases)
Thanks,

data have;
input ID (Time1 Time2 Time3) ( & \$10.);
cards;
1234 CURRENT Current Current
5678 Current Current3 Current
3456 Current Past Current
2345 Current CURRENT Current2
;
run;
Super User
Posts: 10,761

## Re: Do loop question

``````data want;
set have;
array x{*} time1-time12;
flag='CURRENT' in x;
run;``````
Contributor
Posts: 39

## Re: Do loop question

Hi Astounding, Reeza, Zaki, PGStats and Ksharp,

Many thanks to veryone of you.

Reponses are great!

Dunga

Regular Contributor
Posts: 244

## Re: Do loop question

Hi Xia, your code is very helpful, but it is pointing only to one variable and exact text (CURRENT in this case). Can you please tell me the code for Two variable ('Current' and 'Past' in this case) with UPCASE option? I mean flag it when either Current or Past exist regardless of upper or lower case. Thanks.

data have;
input ID (Time1 Time2 Time3) ( & \$10.);
cards;
1234 CURRENT Current Current
5678 Current Current3 Current
3456 Current Past Current
2345 Current CURRENT Current2
;
run;

data want1;
set have;
array x{*} time1-time3;
flag='CURRENT' in x;
run;
Super User
Posts: 10,761

## Re: Do loop question

[ Edited ]

Opps. Missing something.

`data have;input ID (Time1 Time2 Time3) ( & \$10.);cards; 1234 CURRENT Current Current5678 Current Current3 Current3456 Current Past Current2345 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;`

Regular Contributor
Posts: 244

## Re: Do loop question

Thanks Xia, It worked for me perfect!!!
🔒 This topic is solved and locked.