BookmarkSubscribeRSS Feed
Jepi
Calcite | Level 5

I want to group animals from 3 habitat categories in the data. I'm not getting an error message. However, the output is incorrect. The trash data set is filled with observations that should have been categorized and the created variables all equal 0. The flag is not executing either.

 

DATA  ANIMAL_CLASS   TRASH_CLASS;

   SET TOTAL;

FORMAT OCEAN 1.   FARM 1. SAFARI 1. FLAG $10.;

ARRAY WORLD   ANIMAL_CATEGORY_1 - ANIMAL_CATEGORY_3;

DO OVER WORLD;

 

IF WORLD IN("DOLPHIN", "WHALE", "SHARK","SEA TURTLE","FISH","OCTOPUS","SEALS","SQUID")

             THEN OCEAN = 1;

                       ELSE OCEAN = 0 ;

IF WORLD IN ("CATTLE","PIG","CHICKEN","SHEEP","HORSE","GOAT","COW") THEN FARM = 1;

         ELSE FARM = 0;

IF WORLD IN("LION","ELEPHANT","GIRAFFE","RHINOCEROS","ZEBRA","CHEETAH") THEN SAFARI = 1;

       ELSE SAFARI = 0;

 

IF WORLD IN ("LION","ELEPHANT","SHEEP","GOAT","WHALE") THEN FLAG = "TOP 5";

 

END;

 

IF OCEAN GE 1 OR FARM GE 1 OR SAFARI GE 1 OR FLAG = "TOP 5" THEN OUTPUT ANIMAL_CLASS;

       ELSE OUTPUT TRASH_CLASS;

 

RUN;

 

3 REPLIES 3
ballardw
Super User

How about providing some example data and what you expect the result to be.

 

The way your code behaves is the result is only for the variable ANIMAL_CATEGORY_3. The results of that comparison is overwriting what ever the result of the first comparison might be.

Run this code an check the log to see the behavior.

data junk;
   x1 = 1;
   x2 = 2;
   x3 = 3;
   array w x1-x3;
   do over w;
     if w in ( 1, 2) then result=1;
        else result=0;
     if w in (5,6) then result2=1;
        else result2=0;
     put w= result= result2;
   end;
run;

Since you are comparing 3 variables to 3 lists for membership you actually need a 3 result variables for each comparison so you can which variable value appeared in which list.

 

To provide any sort of solution we need to know what you expect for output. It might also help to describe how you are using the result.

 

 

Additionally the IN operator is a test for equality.The spelling of your variable value must be exactly as appears in a list.

If the actual value of the variable is "lion" it will not match the "LION" used in your comparison.

If your value has leading spaces such as " LION" it will not match "LION".

If your value is misspelled such as "LOIN" it will not match "LION".

 

ChrisNZ
Tourmaline | Level 20

1. Please format your code properly bu using the correct icon.

   Also, align and indent your code.
   Also, it's hard to read all uppercase.
   You could try to use uppercase for variable/table names and lower case for language keywords.

2. The 3 iterations of the loop overwrite each other.

    So only ANIMAL_CATEGORY_3 will decide on the value of the numeric flags.

3. Have you checked the case of the strings and the leading spaces?

 

Peter_C
Rhodochrosite | Level 12
Try removing the ELSE statements that appear inside the DO OVER WORLD do-group

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