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

Hello,

 

I have another array question.

 

I'm trying to identify records that have an observation of 'X30' in them, but only if other observations in the variable suite (diagnosis codes 2-25) do not contain 'T67'. I tried a few variations of the code below, but I'm not well-versed in using arrays. The code below gives me ERROR 390-185: Expecting an relational or arithmetic operator.

 

Thank you

 

DATA LIB.D0521;
SET LIB.D0521;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
DO i = 1 TO 25;
IF DX(i) =: 'X30' AND DX(i) NOT 'T67' THEN X30_ONLY_DIAGS=1;
END;
RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You have syntax error and a logic error.

Syntax:  The unary NOT operator wants a boolean value, not a string, as its argument.

Logic:  You need to test all of the codes to see if none of them are T67.

DATA LIB.D0521;
  SET LIB.D0521;
  ARRAY DX DIAG_CODE_1-DIAG_CODE_25 ;
  anyX30 =0;
  anyT67 =0;
  DO i = 1 TO 25;
    IF DX(i) =: 'X30' then anyx30=1;
    IF DX(i) =: 'T67' THEN anyt67=1;
  END;
  if anyx30 and not anyt67 ;
RUN;

View solution in original post

6 REPLIES 6
ballardw
Super User

If the log shows an underline under the value 'T67' that is because NOT as an operation expects something SAS will treat as a logical comparison. That would mean an expression like  not(somevar = 25) or a numerical variable since SAS will treat values of 0 and missing as False and most other numeric values as true.

 

So instead of

DX(i) NOT 'T67'

You may have wanted

DX[i] ne 'T67'

Maybe. 

If you want to search all members of the array you might want

if dx(i) =:'X30' and index( catx(',', of dx(*)),'T67') = 0 then X_30_only_diags=1;

The Index function returns the position of the second argument, the T67, in the first argument, the Catx function result, or 0 if not present.

The catx will return a comma delimited list of the values of the DX array with leading/trailing spaces removed.

Angmar
Obsidian | Level 7

Thank you, but neither worked.

 

They both returned the same number of records (around 2900 out of a possible 3300), but they still contained observations of T67.

 

Thank you again, and I appreciate your suggestion.

SASKiwi
PROC Star

I suspect you are wanting to know if T67 is in ANY of the array variables, in which case you could count up the number of occurrences. If ALL of your 25 codes start with X30 (X30_Count = 25), they can't possibly include T67 as well.

 

DATA LIB.D0521;
SET LIB.D0521;
ARRAY DX (25) DIAG_CODE_1-DIAG_CODE_25 ;
X30_Count = 0;
T67_Count = 0;
DO i = 1 TO 25;
if DX(i) = 'T67' then T67_Count + 1;
IF DX(i) =: 'X30' THEN X30_Count + 1;
END;
RUN;

 

 

Tom
Super User Tom
Super User

You have syntax error and a logic error.

Syntax:  The unary NOT operator wants a boolean value, not a string, as its argument.

Logic:  You need to test all of the codes to see if none of them are T67.

DATA LIB.D0521;
  SET LIB.D0521;
  ARRAY DX DIAG_CODE_1-DIAG_CODE_25 ;
  anyX30 =0;
  anyT67 =0;
  DO i = 1 TO 25;
    IF DX(i) =: 'X30' then anyx30=1;
    IF DX(i) =: 'T67' THEN anyt67=1;
  END;
  if anyx30 and not anyt67 ;
RUN;
Angmar
Obsidian | Level 7

I see - thank you!

andreas_lds
Jade | Level 19

Do yourself a favour and don't overwrite datasets during code development, to avoid having to restore the dataset to its original state.

 

Could you please post some obs of the dataset your are processing? That may allow us to spot issues with the code more easily.

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
  • 6 replies
  • 1465 views
  • 3 likes
  • 5 in conversation