BookmarkSubscribeRSS Feed
JayP
Calcite | Level 5

Q. Write the data step that creates a variable that should have the number of animals that are 200 kg or heavier than 200 kg.

My Ans - This code is not giving me desired results. Any modification on what is the issue ASAP will be of great help.

 

data animal_weight;
input animals $ weight;
datalines;
dog 19
cat 14
mice 0.25
camel 575
pigeon 0.57
cow 475
goat 18
fish 4
horse 450
donkey 330
monkey 23
lion 580
rat 0.69
rabbit 2.5
Pig 235
crow 1.8
;
run;
data sum; set animal_weight;
count=0;
array animl(*)_numeric_;
do i = 1 to 16;
if animl(i) >= 200 then count + animl(i);
proc print data=sum;
end;
run;

quit;

3 REPLIES 3
PGStats
Opal | Level 21

Revise the notion of implicit looping in the data step. This would work:

 

data over200; 
set animal_weight end=done;
count + (weight >= 200);
if done then output;
keep count;
run;

Explain the role of each statement to deserve a good mark.

PG
Shmuel
Garnet | Level 18

Your first step creates a table where each animal holds one row;

 

You cannot use array to deal with different rows;

You can use an array only to deal with a set of variables in the same row.

 

Next code will do what you ask:

data want;

  set animal_weight  end=eof;

        retain count  sum_weight  0;

        if weight GE 200 then do;

             sum_weight = sum(of  sum_weight , weight);

             count+1;

        end;

        if eof then put sum_weight=  count=;

run;

Astounding
PROC Star

I'm heavily biased toward using a DATA step, but I would switch for this task:

 

proc sql;

select count(*) from animal_weight where weight >= 200;

quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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