DATA Step, Macro, Functions and more

why 'if 0 then set' still copy an empty record?

Accepted Solution Solved
Reply
Respected Advisor
Posts: 3,124
Accepted Solution

why 'if 0 then set' still copy an empty record?

Dear Data step user

data b;

if 0 then set sashelp.class;

run;

proc print ;run;

since it is never actually executed, why there is still an empty record been created?

Thanks,

Haikuo


Accepted Solutions
Solution
‎04-07-2012 12:17 AM
PROC Star
Posts: 7,363

Re: why 'if 0 then set' still copy an empty record?

A nice explanation of what occurs when can be found in the following paper: http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf

The pdv is populated first, thus the five variables exist.  One loop was completed, thus one record has to be output.  If you want to do something similar, but not have any loop occur, you could run:

data b;

  if 0;

  set sashelp.class;

run;

That way, you will still get 5 variables, but 0 records.

View solution in original post


All Replies
Solution
‎04-07-2012 12:17 AM
PROC Star
Posts: 7,363

Re: why 'if 0 then set' still copy an empty record?

A nice explanation of what occurs when can be found in the following paper: http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf

The pdv is populated first, thus the five variables exist.  One loop was completed, thus one record has to be output.  If you want to do something similar, but not have any loop occur, you could run:

data b;

  if 0;

  set sashelp.class;

run;

That way, you will still get 5 variables, but 0 records.

Respected Advisor
Posts: 3,124

Re: why 'if 0 then set' still copy an empty record?

Thanks, Art. That was indeed great paper, I have come across it long before I know anything about pdv, and of course I failed to register. Now everything makes sense, Yeah!

BTW, It has also been known to me that if just wanting the structure, (obs=0) will do, too.

Haikuo

Super User
Super User
Posts: 6,500

Re: why 'if 0 then set' still copy an empty record?

You end up with one record because the data step has looped once and output one record.

See how many records you get with the trivial step.  data a; run;

To get 0 records you will need to do something to stop it from outputting the record.

Here are some ways:

data b; set sashelp.class(obs=0); run;

data b; if 0 then set sashelp.class; stop; run;

data b; set sashelp.class; if 0 then output; run;

Respected Advisor
Posts: 3,124

Re: why 'if 0 then set' still copy an empty record?

Thanks, Tom. Your examples are very helpful! I especially like the third one: if 0 then output. really wicked me out.

Haikuo

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 4315 views
  • 4 likes
  • 3 in conversation