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

If i want to create several arrays with sequential numbers, like the following...

 

Data Want;

     set Have;

          array Mitch[*] Temp1-Temp10;

          array Ed [*] Number1-Number10;

          array Phil[*]  Letter1-Letter10;

 

****Other code using the arrays;

run;

 

 

Do the Variables of each array have to be contiguous?  or only sequential in their placement in regards to the previous member of the array? Or does neither matter so long as there is a valid variable name for each array element somewhere in the table (i.e. There exists somewhere in the table, regardless of position, Temp1 Temp2 Temp3.... Temp9 Temp10?

 

I have tables such that my variables are similar to the following.

 

Temp1 Temp2 Temp3 Temp4 Number1 Number2 Number3 Letter1 Letter2 Letter3 Number4 Number5 Temp5 Temp6 Temp7 Letter4 Letter5 Letter6 Temp8 Number6 Letter7.... etc

 

where the individual arrray numbers are sequential in the order of the variables, but not contiguous, as they are broken up by variables of other arrays.  

Will the above code create the correct arrays?  

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@mcook wrote:

 

 

Do the Variables of each array have to be contiguous?  or only sequential in their placement in regards to the previous member of the array? Or does neither matter so long as there is a valid variable name for each array element somewhere in the table (i.e. There exists somewhere in the table, regardless of position, Temp1 Temp2 Temp3.... Temp9 Temp10?

 


If you explicitly list the variables SAS uses those to reference the variables, your index is continuous and references your variables in the order they're listed. The methods for variable short cut lists are in the link below and it's worth understanding the difference between the single hyphen and the double hyphen. 

 

array _temp_array(4) temp3 temp2 temp4 temp1;

In the array above, _temp_array(1) will refer to the temp3 variable. There was a case a few days ago where this type of methodology was used to answer the question more efficiently.

 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

If you do not explicitly list your array variables then it assumes you're creating a set of variables with the array name. 

array example(4);

This creates four variables, example1 - example4. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

The code shown will correctly create/reference the variables you want. 

View solution in original post

3 REPLIES 3
Reeza
Super User

@mcook wrote:

 

 

Do the Variables of each array have to be contiguous?  or only sequential in their placement in regards to the previous member of the array? Or does neither matter so long as there is a valid variable name for each array element somewhere in the table (i.e. There exists somewhere in the table, regardless of position, Temp1 Temp2 Temp3.... Temp9 Temp10?

 


If you explicitly list the variables SAS uses those to reference the variables, your index is continuous and references your variables in the order they're listed. The methods for variable short cut lists are in the link below and it's worth understanding the difference between the single hyphen and the double hyphen. 

 

array _temp_array(4) temp3 temp2 temp4 temp1;

In the array above, _temp_array(1) will refer to the temp3 variable. There was a case a few days ago where this type of methodology was used to answer the question more efficiently.

 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

If you do not explicitly list your array variables then it assumes you're creating a set of variables with the array name. 

array example(4);

This creates four variables, example1 - example4. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

The code shown will correctly create/reference the variables you want. 

Tom
Super User Tom
Super User

The order the variables exist in the dataset, or really the PDV (the program data vector), has nothing to do with the order that they are listed in the ARRAY statement.  Those are two independent things.  But there is some interaction. For example if the ARRAY statement is the first place you reference those variables then they will be placed into the PDV in the same order since that will be the order that the compiler sees them.

 

In your case the order of the variables in the array is being generated by the use of variable lists.  You are using numeric suffixed lists of names. If some of those variables did not exists before the ARRAY statement then they will be created by the ARRAY statement.   So

array Mitch Temp1-Temp10;

is the same as

array Mitch Temp1 Temp2 Temp3 Temp4 Temp5 Temp6 Temp7 Temp8 Temp9 Temp10;

There are other ways to make variable lists.  For example you could use the colon wildcard which allows you to select variables based on the prefix of the name.  So if you used:

set Have;
array Mitch  Temp: ;

Then MITCH[] will contain all of the variables with names that start with TEMP.  And the order will be based on the order they existed in HAVE.  So depending on what is in HAVE that list might be something like this:

array Mitch temp5 temp1 temporary temperature ; 

 

PaigeMiller
Diamond | Level 26

Do the Variables of each array have to be contiguous?


No, they can be anywhere in your data set.

--
Paige Miller

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
  • 883 views
  • 0 likes
  • 4 in conversation