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

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
novinosrin
Tourmaline | Level 20

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.

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

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

 

Patrick
Opal | Level 21

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

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.

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
  • 3 replies
  • 1422 views
  • 0 likes
  • 4 in conversation