BookmarkSubscribeRSS Feed
jdchang
Fluorite | Level 6

Hi all,

 

So I'm looking at a dataset similar to this:

 

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;

 

In this case I'm trying to detect whether var1 - var4 has an "e" or "f" in it and if x1 - x4 has a "7" or "2" in it. My code to process this is similar to the following:

 

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;

Now my problem, I start by initializing each cond# to 0, but once the do loop meets the first valid condition (for either var[i] or x[i]) then the cond# will equal 1 from that point on and won't reset to 0. I don't think I can reset to 0 in the do loop as the cond# value would switch to 0 upon meeting a new var[i] or x[i] that does not meet the criteria. 

 

Any idea how I might be able to get the cond# back to 0? Or if I'm wrongly implementing my code? Thanks!

 

This is a simplified version of what I'm working on, but the structure and implementation of the code is the same. What's different is the number of conditional values, number of array variables, and number of observations.

2 REPLIES 2
kiranv_
Rhodochrosite | Level 12

why do u want to reset to 0

Astounding
PROC Star

There's nothing in the simplified code that you have shown that would do this.  In the more complex version:

 

  • Is there a RETAIN statement?
  • Is there accumulation, such as cond1 + 1
  • Are there any _temporary_ arrays?

There isn't an apparent reason to set COND1 and COND2 to zero.  Leaving them as missing would be OK, at least for the abbreviated code that you posted.  In fact, there is no apparent reason for adding COND1 and COND2 to an array.  The array name is never used.

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
  • 2 replies
  • 493 views
  • 0 likes
  • 3 in conversation