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;
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
data combo;
if _N_=1 then set a;
set b;
run;proc print;run;
Result is :
1 | A | F | |
3 | A | R | |
2 | A | G |
data combo2;
put _n_;
if 1 then set a;
set b;
run; proc print;run;
result is:
1 | A | F | |
3 | T | R |
Why is this different?
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
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'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
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.