## _N_ = 1 and Arrays

Solved
Frequent Contributor
Posts: 129

# _N_ = 1 and Arrays

Hi,

Could someone briefly elucidate the use of _N_ =1 with arrays? Thanks.

```Data score ;
array ans{10} \$1 ;
array key{10} \$1 _temporary_  ;
*Load the temporary array elements*
If _n_ = 1 then do Ques = 1 to 10 ; *_n_ = 1 only does first line of the key *
I		input key{Ques} \$1. @ ;
end ;

Input ID (Ans1-Ans10) (\$1.) ;
Rawscore = 0 ;

*Score the Test*
do Ques = 1 to 10 ;
Rawscore + (key(Ques) eq Ans(Ques)) ;
end ;
Percent = 100*Rawscore ;
keep ID Rawscore Percent ;
Datalines ; ```

Accepted Solutions
Solution
‎05-09-2018 08:08 PM
PROC Star
Posts: 1,784

## Re: _N_ = 1 and Arrays

Posted in reply to ManitobaMoose

I saw your other thread earlier with a response from @PaigeMiller and I was feeling too lazy. Anyway, when sas executes for the 1st time as it loops past the data statement the automatic variable iteration counter increments to 1, the If _n_=1 evaluates to true for the loop do ques… to execute to populate the temporary array key1-key10 as defined above reading the datalines. The temporary array by default retains values across iterations of the dataset without reinitializing the values to missing and so the values are available for processing until end of file . This is useful when processing binary values resulting from (key(Ques) eq Ans(Ques)) adding to the rawscore.

Basically you just need to load the values of temp array only once at least in your process as I believe there is only line of values to be read for the key temporary array. Hope this helps.

All Replies
PROC Star
Posts: 8,164

## Re: _N_ = 1 and Arrays

Posted in reply to ManitobaMoose

Your code is reading the 10 key values but, after the 10th, is expecting to read the first data record from the first data line. Is that what you wanted?

An example dataset would help the forum to understand what you're trying to do.

Art, CEO, AnalystFinder.com

Posts: 4,736

## Re: _N_ = 1 and Arrays

Posted in reply to ManitobaMoose

The keyword _temporary_ makes this a temporary array. Values in temporary array elements are retained.

The loop in IF _N_=1 populates the temporary array with values which then get retained for all later iterations of the data step.

Alternatively the following code would result in the same.

`array key{10} \$1 _temporary_  ('1','2','3','4','5','6','7','8','9','10');`
Solution
‎05-09-2018 08:08 PM
PROC Star
Posts: 1,784

## Re: _N_ = 1 and Arrays

Posted in reply to ManitobaMoose

I saw your other thread earlier with a response from @PaigeMiller and I was feeling too lazy. Anyway, when sas executes for the 1st time as it loops past the data statement the automatic variable iteration counter increments to 1, the If _n_=1 evaluates to true for the loop do ques… to execute to populate the temporary array key1-key10 as defined above reading the datalines. The temporary array by default retains values across iterations of the dataset without reinitializing the values to missing and so the values are available for processing until end of file . This is useful when processing binary values resulting from (key(Ques) eq Ans(Ques)) adding to the rawscore.

Basically you just need to load the values of temp array only once at least in your process as I believe there is only line of values to be read for the key temporary array. Hope this helps.

☑ This topic is solved.

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

Discussion stats
• 3 replies
• 166 views
• 0 likes
• 4 in conversation