BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

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;

7 REPLIES 7
PGStats
Opal | Level 21

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
ZRick
Obsidian | Level 7

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?

PGStats
Opal | Level 21

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
PGStats
Opal | Level 21

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
Ksharp
Super User

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


Linlin
Lapis Lazuli | Level 10

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.

Haikuo
Onyx | Level 15

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

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

Haikuo

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2041 views
  • 0 likes
  • 5 in conversation