I want to use arrays but confused on how to create and use it. While reading about arrays, I came across this explanation:
array newvars [3] test1-test3 (45 23 21);
newvars[3] will return 21
while I get that newvars[3] returns 21, what I don't get is test1-test3. what are they for? it's not even needed since I can get the value from the array by referencing it, e.g. newvars[2].
more importantly, I am working on this logic:
I want to create an array of strings and use that array inside a do loop. please help me create the array. Here's my initial stab at it:
array oldarray[4] ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
do i = 1 to 4;
*use oldarray[i] here to manipulate a variable;
end;
please help. I only need how to create the array.
@jffeudo86 wrote:
I want to use arrays but confused on how to create and use it. While reading about arrays, I came across this explanation:
array newvars [3] test1-test3 (45 23 21);
newvars[3] will return 21
while I get that newvars[3] returns 21, what I don't get is test1-test3. what are they for? it's not even needed since I can get the value from the array by referencing it, e.g. newvars[2].
more importantly, I am working on this logic:
I want to create an array of strings and use that array inside a do loop. please help me create the array. Here's my initial stab at it:
array oldarray[4] ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
do i = 1 to 4;
*use oldarray[i] here to manipulate a variable;
end;
please help. I only need how to create the array.
There are several ways to use an array. One is to use an array to create new variables:
Array a {5} ;
will create numeric variables A1, A2, A3, A4, A5.
Array a{3} test1-test3 ; may do one of two things that depend on the current data set. If the Variables test1, test2 and test 3 already exist in the data then you can use array shorthand a[1] to refer to the existing variable Test1. If the variables to not currently exist then the array statement creates them and you can reference them with the array shorthand.
array oldarray[4] ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
with strings you need more work, first you need to tell SAS the values are character and how long they will be
Array oldarray{4} $ 25 for example to tell SAS that the values may be up to 25 characters.
If you don't want the variables in the output data set you can create a temporary array:
array oldarray[4] $ 25 _temporary_ ('word1' 'two words' 'more words' 'still more words');
Note that _temporary_ arrays values persist across data step record boundaries. So be careful about assigning values to the array elements as you may have unintended consequences.
To create an array without variables, use the keyword _TEMPORARY_:
array oldarray[4] $ 20 _temporary_ ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
Note that array elements must be defined as numeric or character, or else they will default to numeric. In the case of a _temporary_ array, there may be some flexibility there, but I can't test it right now.
Also note, elements within a _temporary_ array maintain their values from one observation to the next. If you want them to start over from some baseline set of values on every observations, you need to re-set them. Any changes you make stay changed as you move from one observation to the next.
@jffeudo86 wrote:
I want to use arrays but confused on how to create and use it. While reading about arrays, I came across this explanation:
array newvars [3] test1-test3 (45 23 21);
newvars[3] will return 21
while I get that newvars[3] returns 21, what I don't get is test1-test3. what are they for? it's not even needed since I can get the value from the array by referencing it, e.g. newvars[2].
more importantly, I am working on this logic:
I want to create an array of strings and use that array inside a do loop. please help me create the array. Here's my initial stab at it:
array oldarray[4] ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
do i = 1 to 4;
*use oldarray[i] here to manipulate a variable;
end;
please help. I only need how to create the array.
There are several ways to use an array. One is to use an array to create new variables:
Array a {5} ;
will create numeric variables A1, A2, A3, A4, A5.
Array a{3} test1-test3 ; may do one of two things that depend on the current data set. If the Variables test1, test2 and test 3 already exist in the data then you can use array shorthand a[1] to refer to the existing variable Test1. If the variables to not currently exist then the array statement creates them and you can reference them with the array shorthand.
array oldarray[4] ('word1' 'two words' 'more words' 'still more words'); /* i don't need variables */
with strings you need more work, first you need to tell SAS the values are character and how long they will be
Array oldarray{4} $ 25 for example to tell SAS that the values may be up to 25 characters.
If you don't want the variables in the output data set you can create a temporary array:
array oldarray[4] $ 25 _temporary_ ('word1' 'two words' 'more words' 'still more words');
Note that _temporary_ arrays values persist across data step record boundaries. So be careful about assigning values to the array elements as you may have unintended consequences.
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.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.