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 ;
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.
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
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');
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.