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-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
  • 7 replies
  • 1577 views
  • 0 likes
  • 5 in conversation