BookmarkSubscribeRSS Feed
SAIKDE
Calcite | Level 5

data one;
if _n_ = 1 then do i = 1 to 4;
set sashelp.class;
end;
value = _n_;
output;
run;

 

I want to understand why there are two outputs. As per the logic, there should be one output, when _n_ = 2, all the column values should go missing. then why the first observation is being retained?

7 REPLIES 7
Kurt_Bremser
Super User

Maxim 2: Read the Log.

 69         data one;
 70         if _n_ = 1 then do i = 1 to 4;
 71         set sashelp.class;
 72         end;
 73         value = _n_;
 74         output;
 75         run;
 
 NOTE: DATA STEP stopped due to looping.
 NOTE: There were 4 observations read from the data set SASHELP.CLASS.

Without the data step being so intelligent, this code would run forever, with great CPU consumption.

Why?

In the first iteration, it reads 4 observations, and outputs the 4th.

In the next (and every other possible iteration), nothing is read, preventing an end-of-dataset from terminating the step. But the data step itself realizes at the end of the second iteration that it would never reach an end (because the dataset observation pointer did not change) and shuts down on its own. This happens after the explicit OUTPUT has been executed for a second time; since all variables coming from input datasets are automatically retained, the observation looks identical to the first, with the exception of i. This variable is not from a dataset, and is therefore set to missing at the beginning of a data step iteration.

 

BTW, even if you omit the explicit OUTPUT, the implicit output will also happen twice before the data step stops.

AMSAS
SAS Super FREQ

The question is what are you attempting to achieve?

Astounding
PROC Star

When SAS reads from a SAS data set (such as using a SET statement), it automatically retains all variables coming from the SAS data set.  They are never re-set to missing.

Tom
Super User Tom
Super User

On the second iteration the variable _N_ will be set to 2 and variables I and VALUE that are defined by the data step will be set to missing.  But the variables coming from the input dataset are NOT set to missing.  That is how the data step works.  It is what makes one to many merges work.

 

If you only want the 4th observation then use some other method.

data four;
  set sashelp.class (obs=4 firstobs=4);
run;

data four;
  set sashelp.class;
  if _n_=4;
run;
mkeintz
PROC Star

You are using a conditional SET statement.  As explained by others, variables read in by a SET (or a MERGE) statement are not automatically reset to missing, and only change values when they are overwritten by the next SET or MERGE accessing those variables.  But in your case, there is no use of SET except for _N_=1.

 

Of course, you could take on the activity of resetting to missing on your own, as below - which would generate the missing values you expected for the second observation.

 

data one;
  if _n_ = 1 then do i = 1 to 4;
    set sashelp.class;
  end;
  value = _n_;
  output;
  call missing(of _all_);
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
dexcort2020
Obsidian | Level 7
four' one;
  if _n_ = 1 then do i = 1 to 4;
    set sashelp.class;
  end;
  out = _n_;
  output;
  run(of _all_);
run;
ballardw
Super User

@dexcort2020 wrote:
four' one;
  if _n_ = 1 then do i = 1 to 4;
    set sashelp.class;
  end;
  out = _n_;
  output;
  run(of _all_);
run;

??????????????????

Not basic SAS syntax, so where is this coming from?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 672 views
  • 2 likes
  • 8 in conversation