BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Quartz | Level 8

_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 ; ```
1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

Re: _N_ = 1 and Arrays

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.

3 REPLIES 3
Opal | Level 21

Re: _N_ = 1 and Arrays

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

Opal | Level 21

Re: _N_ = 1 and Arrays

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');`
Tourmaline | Level 20

Re: _N_ = 1 and Arrays

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.

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