| Date: | Tue, 31 Oct 2000 16:00:54 -0500 |
| Reply-To: | Howard Schreier <Howard_Schreier@ITA.DOC.GOV> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Howard Schreier <Howard_Schreier@ITA.DOC.GOV> |
| Subject: | Re: SAS/Base: automatic variable _n_ behavior |
|---|
This is reminiscent of Bob Virgile's contest problems.
The DATA step with the explicit loop will actually begin execution twice,
not once.
Try inserting the statement
put _N_=;
between the DO and SET statements. The log will then contain 11 dumps of the
automatic variable. The first ten will have the value 1 and the eleventh
will have the value 2.
The DATA step here does not terminate until the SET fails to return a new
observation. In this case that happens on the first iteration of the DO loop
during the second iteration of the DATA step.
This is a technical clarification only; I agree with Greg's suggestion.
On Tue, 31 Oct 2000 13:14:44 -0600, Greg Woolridge <greg.woolridge@TAP.COM>
wrote:
>_n_ is the number of times the data step has begun executing, which does
>not necessarily corrspond to the observation being read from the data set,
>as you have discovered. In the creation of B you have the SET statement
>within a DO loop. The data step executes only 1 time and the DO loops as
>many times as needed to read all observations. Since the data step
>executes only 1 time, _n_ is never incremented. I would suggest using a
>counter instead as below:
>
>data B ;
> do until (EOF) ;
> set A end=EOF ;
> N + 1;
> output ;
> end ;
>run ;
>
>
>Greg M. Woolridge
>Manager, Study Programming
>TAP Pharmaceutical Products Inc.
>e-mail: greg.woolridge@tap.com
>phone: 847-582-2332
>fax: 847-582-2403
>
>
>
> Bill Knowlton
> <Bill_Knowlton@B To: SAS-L@LISTSERV.UGA.EDU
> AXTER.COM> cc:
> Sent by: "SAS(r) Subject: SAS/Base:
automatic variable _n_ behavior
> Discussion"
> <SAS-L@LISTSERV.
> UGA.EDU>
>
>
> 10/31/00 12:33
> PM
> Please respond
> to Bill_Knowlton
>
>
>
>
>
>Hi all,
>
>In the code included below, 'was wondering if anyone can explain why the
>automatic variable _n_ doesn't increment for the code generating dataset B
>but does for the code generating dataset C.
>
>What does _n_ represent? I had thought it represented the current
>observation number read from the input dataset. Is there some other
>automatic variable that holds this value? I've had no luck in finding a
>description of _n_ in the SAS help, other than that it is a reserved name.
>
>Thanks for any insights,
>
>-Bill
>
>
>data A ;
> do i = 1 to 10 ;
> output ;
> end ;
>run ;
>
>data B ;
> do until (EOF) ;
> set A end=EOF ;
> N = _n_ ;
> output ;
> end ;
>run ;
>
>data C ;
> set A ;
> N = _n_ ;
> output ;
>run ;
|