I did not write this but want to use it from an example. Please help me to understand this code.
Thanks
data new_data;
set old_data;
array VARS [*] VAR1 VAR2 VAR3
array NVARS [*] nVAR1 nVAR2 nVAR3
do k1 = 1 to dim(VARS);
NVARS[k1] = VARS[k1];
end;
drop k1;
data new_data;
set old_data;
*declare an array called VARS, which refers to VAR1-VAR3;
array VARS [*] VAR1 VAR2 VAR3
*declare an array called NVARS, which refers to NVAR1-NVAR3;
array NVARS [*] nVAR1 nVAR2 nVAR3
*do loop to go over the array;
*k1 is the loop counter;
*dim() returns the number of items in the array, 3 in this case. It tells how many loops to go through;
do k1 = 1 to dim(VARS);
*copies values from VARS to NVARS;
NVARS[k1] = VARS[k1];
end;
*removes index variable from final data set;
drop k1;
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
@Emma_at_SAS wrote:
I did not write this but want to use it from an example. Please help me to understand this code.
Thanks
data new_data; set old_data; array VARS [*] VAR1 VAR2 VAR3 array NVARS [*] nVAR1 nVAR2 nVAR3 do k1 = 1 to dim(VARS); NVARS[k1] = VARS[k1]; end; drop k1;
It appears that variable nVAR1 is being created and is exactly equal to VAR1, and so on. I suspect nVAR1 is numeric while VAR1 is character, but I don't know for sure as I don't have the corresponding data set OLD_DATA.
This code won't work as there are at least two semi-colons missing. Please post code that works, if you can, as in this situation that shouldn't be hard.
Basic bit: The array statement defines the name of the array Vars or Nvars and then lists the names of the variables that will be in the array in position order. In order means that when you provide an integer index value as in Vars[3] the variable that appears third in the list is used.
The (*) in the definition tells SAS to set the number of elements in the array to match the number of variable names provided.
You can reference existing variables and/or new variables creating them with the definition if they did not exist.
The DIM function returns the number of elements or variables that are defined for the given array. This helps make things flexible when you use variable lists where you may not know (or don't want to count them). So the Do K1 loop iterates values of K1 from 1 to 3.
The: Nvars[K1] = Vars[k1];
is copying the values from the first array into the second. Nvar1 will equal Var1, Nvar2 will equal Var2 and so on after the loop finishes executing.
If the variables Nvar1 to Nvar3 did not exist in your old data you could also use an array definition to create them by using:
Array Nvar(3); which explicitly limits the number of elements to 3 and would create numeric variables name Nvar1, Nvar2 and Nvar3. You would then reference the array with Nvar[indexvalue] instead of Nvars[indexvalue].
data new_data;
set old_data;
*declare an array called VARS, which refers to VAR1-VAR3;
array VARS [*] VAR1 VAR2 VAR3
*declare an array called NVARS, which refers to NVAR1-NVAR3;
array NVARS [*] nVAR1 nVAR2 nVAR3
*do loop to go over the array;
*k1 is the loop counter;
*dim() returns the number of items in the array, 3 in this case. It tells how many loops to go through;
do k1 = 1 to dim(VARS);
*copies values from VARS to NVARS;
NVARS[k1] = VARS[k1];
end;
*removes index variable from final data set;
drop k1;
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
@Emma_at_SAS wrote:
I did not write this but want to use it from an example. Please help me to understand this code.
Thanks
data new_data; set old_data; array VARS [*] VAR1 VAR2 VAR3 array NVARS [*] nVAR1 nVAR2 nVAR3 do k1 = 1 to dim(VARS); NVARS[k1] = VARS[k1]; end; drop k1;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.