BookmarkSubscribeRSS Feed
Angmar
Obsidian | Level 7

Hello,

I'm unable to get SAS to return observations when using an 'if' statement including 3 new variables I've made using multiple arrays. The first code (below) works fine and returns ~230 observations. 

DATA LIB.DAD05;
SET DAD.DAD2005;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
IF DX(i) =: 'X47' OR DX(i)=: 'X470' OR DX(i)=: 'X471' OR DX(i)=: 'X472' OR DX(i)=: 'X473' OR DX(i)=: 'X474' THEN UNINTENTIONAL_POISONING=1;
END;
RUN;

 

However, when I use multiple arrays and an 'if' statement with the 3 new variables (below), it's almost as if everything is being overwritten (I suspect that the final array should return 0 observations, just due to dataset variable issues, but the first array has already returned ~230 observations when run on its own).

 

DATA LIB.DAD05;
SET DAD.DAD2005;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
IF DX(i) =: 'X47' OR DX(i)=: 'X470' OR DX(i)=: 'X471' OR DX(i)=: 'X472' OR DX(i)=: 'X473' OR DX(i)=: 'X474' THEN UNINTENTIONAL_POISONING=1;
END;

ARRAY DXA (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO j = 1 TO 25;
IF DXA(j) =: 'T58' THEN CO_POISONING=1;
END;

ARRAY DXB (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO k = 1 TO 25;
IF DXB(k) =: 'Y17' OR DXB(k)=: 'Y170' OR DXB(k)=: 'Y171' OR DXB(k)=: 'Y172' OR DXB(k)=: 'Y173' OR DXB(k)=: 'Y174'
THEN UNDETERMINED_INTENT_POISONING=1;
END;

IF UNINTENTIONAL_POISONING NE 1 OR CO_POISONING NE 1 OR UNDETERMINED_INTENT_POISONING NE 1 THEN DELETE;
RUN;

 

As always, thank you - this community is great.

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You have not shown us a portion of your data, so we can not be sure what is happening. Your explanation also doesn't indicate what result is desired, nor is this a helpful description that we can work from: "it's almost as if everything is being overwritten".

 

Please provide a portion of your data as working SAS data step code, which you can type yourself, or by following these instructions. Do not attach files or provide screen captures. Please explain clearly what you expect to happen on this sample data.

--
Paige Miller
Reeza
Super User

 

DATA LIB.DAD05;
SET DAD.DAD2005;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
     IF DX(i) in: ( 'X47', 'X470', 'X471',  'X472',  'X473',  'X474') THEN UNINTENTIONAL_POISONING=1;
    IF DX(j) =: 'T58' THEN CO_POISONING=1;
    IF DX(k) =: 'Y17' OR DX(k)=: 'Y170' OR DX(k)=: 'Y171' OR DX(k)=: 'Y172' OR DX(k)=: 'Y173' OR DX(k)=: 'Y174'
THEN UNDETERMINED_INTENT_POISONING=1;
END;

IF sum(UNINTENTIONAL_POISONING, CO_POISONING, UNDETERMINED_INTENT_POISONING) =0 THEN DELETE;


RUN;

 

 

  • Don't declare the same array multiple times
  • Don't loop through same array multiple times 
  • Don't use OR when you can use IN: or IN
  • Show an example with data of what isn't meeting expectations.

@Angmar wrote:

Hello,

I'm unable to get SAS to return observations when using an 'if' statement including 3 new variables I've made using multiple arrays. The first code (below) works fine and returns ~230 observations. 

DATA LIB.DAD05;
SET DAD.DAD2005;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
IF DX(i) =: 'X47' OR DX(i)=: 'X470' OR DX(i)=: 'X471' OR DX(i)=: 'X472' OR DX(i)=: 'X473' OR DX(i)=: 'X474' THEN UNINTENTIONAL_POISONING=1;
END;
RUN;

 

However, when I use multiple arrays and an 'if' statement with the 3 new variables (below), it's almost as if everything is being overwritten (I suspect that the final array should return 0 observations, just due to dataset variable issues, but the first array has already returned ~230 observations when run on its own).

 

DATA LIB.DAD05;
SET DAD.DAD2005;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
IF DX(i) =: 'X47' OR DX(i)=: 'X470' OR DX(i)=: 'X471' OR DX(i)=: 'X472' OR DX(i)=: 'X473' OR DX(i)=: 'X474' THEN UNINTENTIONAL_POISONING=1;
END;

ARRAY DXA (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO j = 1 TO 25;
IF DXA(j) =: 'T58' THEN CO_POISONING=1;
END;

ARRAY DXB (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO k = 1 TO 25;
IF DXB(k) =: 'Y17' OR DXB(k)=: 'Y170' OR DXB(k)=: 'Y171' OR DXB(k)=: 'Y172' OR DXB(k)=: 'Y173' OR DXB(k)=: 'Y174'
THEN UNDETERMINED_INTENT_POISONING=1;
END;

IF UNINTENTIONAL_POISONING NE 1 OR CO_POISONING NE 1 OR UNDETERMINED_INTENT_POISONING NE 1 THEN DELETE;
RUN;

 

As always, thank you - this community is great.


 

 

ballardw
Super User

An array addresses variables. If all the variables are the same then use one array. There is no reason to have multiple arrays for the same variables.

 

Since you do not assign any values to those 3 variables for what I suspect are most of the records then they are MISSING values and hence "ne 1".

It would help to describe what you think you want for the output.

 

Read this example data and determine which records you expect in the output. THEN run the data step and see if it matches your expectation.

data junk;
  input val1 val2;
  if val1 ne 1 or val2 ne 1 then delete;
datalines;
1 .
. 1
1 1
. .
;

(Hint: if you expect 3 records in the output the If statement is incorrect)

Kurt_Bremser
Super User

First of all, there's no need to shout at the SAS interpreter. It works very fine with lowercase, which is easier to read.

Second, you define three array fior the same variables; this is not necessary, you can reuse an array as often as you want.

Third, you can do everything in one DO loop.

Fourth, your code will only output observations with no poisoning at all. Is this what you want, or do you want all observations where any poisoning is detected?

data lib.dad05;
set dad.dad2005;
array dx{25} diag_code_1-diag_code25;
do i =1 to 25;
  if dx{i} =: 'X47' or dx(i)=: 'X470' or dx(i)=: 'X471' or dx(i)=: 'X472' or dx(i)=: 'X473' or dx(i)=: 'X474' then unintentional_poisoning = 1;
  if dx(i) =: 'T58' then co_poisoning = 1;
  if dx(k) =: 'Y17' or dx(k)=: 'Y170' or dx(k)=: 'Y171' or dx(k)=: 'Y172' or dx(k)=: 'Y173' or dx(k)=: 'Y174' then undetermined_intent_poisoning = 1;
end;
if sum(unintentional_poisoning,co_poisoning,undetermined_intent_poisoning) le 0;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 945 views
  • 0 likes
  • 5 in conversation