BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dunga
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Try it this way:

 

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

else flag=0;

View solution in original post

11 REPLIES 11
Astounding
PROC Star

Try it this way:

 

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

else flag=0;

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

If whichc('Current', of time1-time12)>0 then flag=1;
Reeza
Super User
Make sure the case matches...
mohamed_zaki
Barite | Level 11
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;
PGStats
Opal | Level 21

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
mlogan
Lapis Lazuli | Level 10
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;
Ksharp
Super User
data want;
 set have;
 array x{*} time1-time12;
 flag='CURRENT' in x;
run;
dunga
Obsidian | Level 7

Hi Astounding, Reeza, Zaki, PGStats and Ksharp,

 

Many thanks to veryone of you.

 

Reponses are great!

 

Dunga

mlogan
Lapis Lazuli | Level 10
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;
Ksharp
Super User

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;

 

mlogan
Lapis Lazuli | Level 10
Thanks Xia, It worked for me perfect!!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1334 views
  • 7 likes
  • 7 in conversation