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

Hi all,

 

For my question I'm using a dataset similar to the one below:

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;

My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.

 

How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?

 

Thanks for any help or ideas you have.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

use boolean and not initialize  array 2*0 which retains across

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
/*	array cond (2) cond1-cond2 ;not needed*/
	
	do i=1 to 4;
	  if not cond1 then   cond1=var[i] in ("e", "f");
	   if not cond2 then  cond2=x[i] in ("7", "2");
	end;
	drop i;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

use boolean and not initialize  array 2*0 which retains across

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
/*	array cond (2) cond1-cond2 ;not needed*/
	
	do i=1 to 4;
	  if not cond1 then   cond1=var[i] in ("e", "f");
	   if not cond2 then  cond2=x[i] in ("7", "2");
	end;
	drop i;
run;
jdchang
Fluorite | Level 6
Thank you!
Reeza
Super User

WHICHC() + OR 

 

data want;
set sample;

cond1 = whichc('e', of var1-var4) | whichc('f', of var1-var4);
cond2 = whichc('7', of x1-x4) | whichc('2', of x1-x4);

run;

This works for me.

 


@jdchang wrote:

Hi all,

 

For my question I'm using a dataset similar to the one below:

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;

My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.

 

How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?

 

Thanks for any help or ideas you have.


 

Reeza
Super User

When you initialize an array, it also does an implicit retain on those variables. You can get around that by not initializing them in the ARRAY statement but after the fact, or not at all unless it's needed.

 


@jdchang wrote:

Hi all,

 

For my question I'm using a dataset similar to the one below:

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;

My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.

 

How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?

 

Thanks for any help or ideas you have.


 

novinosrin
Tourmaline | Level 20

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data want;
set sample;
cond1=^^findc(cats(of var:),'ef');
cond2=^^findc(cats(of x:),'72');
run;
ballardw
Super User

@novinosrin wrote:

data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data want;
set sample;
cond1=^^findc(cats(of var:),'ef');
cond2=^^findc(cats(of x:),'72');
run;

And I think@novinosrin wins obfuscation code of the day.

 

For our OP the FINDC function returns either the number position of the character found or 0. The second ^ (meaning NOT) negates the value of the number returned. Any 0 becomes 1 and any value returned other than 0 becomes 1 in the way SAS does logic operations. Then the first ^ negates the other result switching 0 to 1 and 1 to 0 as needed.

 

I like it.

Then the

 

 

novinosrin
Tourmaline | Level 20

Thank you @ballardw Sir. It's always been a privilege to have learned/learn a lot from you. I have sought your help numerous times and you must be glad I pay  sincere attention. Thank you again 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 1106 views
  • 7 likes
  • 4 in conversation