BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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;

 

 

 


 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Emma_at_SAS
Lapis Lazuli | Level 10
Thank you, PaigeMiller and sorry for the missing semi-colons. I fixed them below. I did not add more info as more details are in the next replies. 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;
ballardw
Super User

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].

Emma_at_SAS
Lapis Lazuli | Level 10
Thank you very much, ballardw. These details were very helpful. This could be the accepted response but I am going to accept the response from Reeza as it is very easy to follow in seconds. But I am adding this note for future visitors to read this post as well. Thank you. i very much appreciate your detailed and quick response.
Reeza
Super User
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;

 

 

 


 

Emma_at_SAS
Lapis Lazuli | Level 10
Thank you very much Reeza!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 916 views
  • 3 likes
  • 4 in conversation