DATA Step, Macro, Functions and more

SET STATEMENT

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

SET STATEMENT


Hi Team,

I was trying to understand this simple set statement and confused a little bit. Could someone help me understand the logic behind this???

I COULD SEE THAT THIS PROGRAM OUTPUTS THE FIRST 10 of THE 19 OBS FROM SASHELP>CLASS!!!!!!!!!!But why is it like that

I thought it gives observations greater than height 65 from the first set and all the obs from the second set!!!


data trial2;

set sashelp.class(keep=height);

if height > 62.5;

set sashelp.class;

run;

This is reproduced from SAS documentation :

data drugxyz;

set trial5(keep=sample);

if sample > 2;

set trial5;

run;

Regards


Accepted Solutions
Solution
‎10-24-2012 04:14 PM
Super User
Super User
Posts: 6,498

Re: SET STATEMENT

955  proc sql noprint ;

956   select count(*) into :nobs

957    from sashelp.class where height > 62.5

958   ;

959  %put nobs=&nobs ;

nobs=      10

960  quit;

Think about what the subsetting IF statement does.

How many times do you think the second SET statement is going to execute?

View solution in original post


All Replies
Solution
‎10-24-2012 04:14 PM
Super User
Super User
Posts: 6,498

Re: SET STATEMENT

955  proc sql noprint ;

956   select count(*) into :nobs

957    from sashelp.class where height > 62.5

958   ;

959  %put nobs=&nobs ;

nobs=      10

960  quit;

Think about what the subsetting IF statement does.

How many times do you think the second SET statement is going to execute?

Super Contributor
Posts: 1,040

Re: SET STATEMENT

Hi,

I guess you showed me what happens with the first set stmnt.

i dint still get the usefulness of the second set stmnt

Regards

Super Contributor
Posts: 1,040

Re: SET STATEMENT

OK GOT U.

It is subsetted and then the same no of observations are taken when u put the second set stmnt.

If the second set statement has a diff dataset than the first then additive effect????right???

Thnks

Super User
Super User
Posts: 6,498

Re: SET STATEMENT

I would be hard fetched to find a practical application for such a contrived program. 

How about you are screening subjects to find those that meet the enrollment criteria.  When you find a subject you need to assign them the next randomization number.  So the first SET statement could be list of subjects in chronological order. The subsetting IF statement  tests if they meet the entrance criteria. The second SET statement will be the list of randomizations. So only subjects that meet the criteria make it past the IF statement. Then the second SET statement adds in the values of the variables from the randomization file (randomization number, treatment, etc.).  So the resulting dataset is the list of randomized subjects with their randomization information.

Super Contributor
Posts: 1,040

Re: SET STATEMENT

Thanks Tom for the detailed information.

BUT running these steps below:

data trial2;

set sashelp.class(keep=height);

if height > 62.5;

set sashelp.class;

run;

I am getting observations in which height is lessthan 62.5...

How can that be possible

Regards

Super User
Super User
Posts: 6,498

Re: SET STATEMENT

Because HEIGHT is a variable in the dataset referenced in the second SET statement. So the value from that record overwrites any value that is already there for the current iteration.

Check the result when you run this data step.

data new ;

  height = 100;

  set sashelp.class;

run;

PROC Star
Posts: 7,360

Re: SET STATEMENT

Karun,

Or, similar to Tom's suggestion,just look at your results and compare them with sashelp.class:

sashelp.class

obs name sex age height weight

1AlfredM1469.0112.5
2AliceF1356.584.0
3BarbaraF1365.398.0
4CarolF1462.8102.5
5HenryM1463.5102.5
6JamesM1257.383.0
7JaneF1259.884.5
8JanetF1562.5112.5
9JeffreyM1362.584.0
10JohnM1259.099.5
11JoyceF1151.350.5
12JudyF1464.390.0
13LouiseF1256.377.0
14MaryF1566.5112.0
15PhilipM1672.0150.0
16RobertM1264.8128.0
17RonaldM1567.0133.0
18ThomasM1157.585.0
19WilliamM1566.5112.0

trial2

169.0AlfredM14112.5
256.5AliceF1384.0
365.3BarbaraF1398.0
462.8CarolF14102.5
563.5HenryM14102.5
657.3JamesM1283.0
759.8JaneF1284.5
862.5JanetF15112.5
962.5JeffreyM1384.0
1059.0JohnM1299.5

Notice that you are only getting 10 records and they match the first 10 records from sashelp.class.  You are getting 10 records because there are 10 records that meet your condition.  However, the 2nd set statement is only enacted if the first one's condition is met.

Thus, there are 10 records that meet your condition but, because of your 2nd set statement, you are actually outputting the first 10 records from sashelp.class.

Respected Advisor
Posts: 3,124

Re: SET STATEMENT

There are several ways to end a data step besides clicking 'break' button. Stop, Abort or nature ending. Your question involves nature ending which is defined by the table finished first within the same data step. In your original scenario, the first table finished first:

data trial2;

set sashelp.class(keep=height);

if height > 62.5;

ct+1;

set sashelp.class;

run;

you can check how many times it actually executes passing through "if height > 62.5;" by adding a counter 'ct'. When it sequentially reaches the end marker of first table, data step stops regardless how many records left to be read in the second one.

As you may start wondering, another nature ending is to end the second one first, then you need to change access method of the first table from 'sequential' to 'random' by using point=, by doing that, you never get to reach the end marker of first table, even after the last record has been read. The following example is to show how the second table ends first, so you can have all 19 records from second table. however, "if height > 62.5;" is literally useless here, as the second table will always rewrite PDV before output.

data trial2;

ct+1;

set sashelp.class(keep=height) point=ct nobs=nobs;

if height > 62.5;

set sashelp.class;

if ct=nobs then ct=0;

run;

Haikuo

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 318 views
  • 0 likes
  • 4 in conversation