DATA Step, Macro, Functions and more

Set a; set b; minimal record number, not applicable?

Reply
Frequent Contributor
Posts: 133

Set a; set b; minimal record number, not applicable?

Should number of records in combo be 2 rather than 3?

Why _N_=1 make the minimal record rule not applicable?

data a;

input x 1. y $2.;

datalines;

1 A

5 T

;

data b;

input x 1. z $2.;

datalines;

1 F

3 R

2 G

;

data combo;

if _N_=1 then set a;

set b;

run;

Respected Advisor
Posts: 4,655

Re: Set a; set b; minimal record number, not applicable?

Even more surprizing if dataset a contains only one observation.

To help understand , try

data combo;

put _n_;

if 1 then set a;

set b;

run;

and then change 1 for 0. Trying this, I conclude that the datastep stops iterating as soon as one of the set or input statements hits an end of file.

PG

PG
Frequent Contributor
Posts: 133

Re: Set a; set b; minimal record number, not applicable?

data combo;

if _N_=1 then set a;

set b;

run;proc print;run;

Result is :

1AF
3AR
2AG

data combo2;

put _n_;

if 1 then set a;

set b;

run; proc print;run;

result is:

1AF
3TR

Why is this different?

Respected Advisor
Posts: 4,655

Re: Set a; set b; minimal record number, not applicable?

The datastep loop iterates as long as it can. When a set statement tries to read beyond end of file, the loop continues to the end, it does not produce a new observation, and it stops.

In the first case, set a is prevented from reading beyond the end of file, so it is set b that tries first.

In the second case, set a is read at every loop, so it tries to read beyond the end of file first.

PG

PG
Respected Advisor
Posts: 4,655

Re: Set a; set b; minimal record number, not applicable?

Or most instructive, try:

data b;

input x 1. z $2.;

datalines;

1 F

3 R

;

data combo;

put "Before " _n_ (_all_) (=);

if not endOfB then set b end=endOfB;

put "After " _n_ (_all_) (=);

if _n_ > 5 then stop;

run;

Intricate...

PG

PG
Super User
Posts: 9,687

Re: Set a; set b; minimal record number, not applicable?

PG's code

data combo;

if 1 then set a;

set b;

run;


is the same with the following code:

data combo;

set a;

set b;

run;


Because A is the smallest table ,so when Data Step reach at the end of A, it

stop working.You only can get two observations.


Ksharp


Super Contributor
Posts: 1,636

Re: Set a; set b; minimal record number, not applicable?

SAS stops when it reaches the end of a dataset. In your example, the end of dataset SAS reaches is the end of dataset b.  " if _n_=1 then set data a;" makes it impossible for SAS to reach the end of dataset a.

Respected Advisor
Posts: 3,124

Re: Set a; set b; minimal record number, not applicable?

Here is a classic paper on this whole topic what you may find helpful:

http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf

Haikuo

Ask a Question
Discussion stats
  • 7 replies
  • 600 views
  • 0 likes
  • 5 in conversation