<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Understanding an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730443#M227450</link>
    <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Wed, 31 Mar 2021 17:49:40 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-03-31T17:49:40Z</dc:date>
    <item>
      <title>Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730438#M227448</link>
      <description>&lt;P&gt;I did not write this but want to use it from an example. Please help me to understand this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 17:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730438#M227448</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-03-31T17:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730443#M227450</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 17:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730443#M227450</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-31T17:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730446#M227453</link>
      <description>&lt;P&gt;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 &lt;STRONG&gt;in position order.&lt;/STRONG&gt; 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.&lt;/P&gt;
&lt;P&gt;The (*) in the definition tells SAS to set the number of elements in the array to match the number of variable names provided.&lt;/P&gt;
&lt;P&gt;You can reference existing variables and/or new variables creating them with the definition if they did not exist.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;The: Nvars[K1] = Vars[k1];&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;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].&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 17:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730446#M227453</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-31T17:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730447#M227454</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial on using Arrays in SAS&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-arrays/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-arrays/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84351"&gt;@Emma_at_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I did not write this but want to use it from an example. Please help me to understand this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 17:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730447#M227454</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-31T17:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730451#M227457</link>
      <description>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&lt;BR /&gt;data new_data;&lt;BR /&gt;set old_data;&lt;BR /&gt;array VARS [*] VAR1 VAR2 VAR3;&lt;BR /&gt;array NVARS [*] nVAR1 nVAR2 nVAR3;&lt;BR /&gt;do k1 = 1 to dim(VARS);&lt;BR /&gt;NVARS[k1] = VARS[k1];&lt;BR /&gt;end;&lt;BR /&gt;drop k1;</description>
      <pubDate>Wed, 31 Mar 2021 17:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730451#M227457</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-03-31T17:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730453#M227458</link>
      <description>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.</description>
      <pubDate>Wed, 31 Mar 2021 17:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730453#M227458</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-03-31T17:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730454#M227459</link>
      <description>Thank you very much Reeza!</description>
      <pubDate>Wed, 31 Mar 2021 18:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-an-array/m-p/730454#M227459</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-03-31T18:00:36Z</dc:date>
    </item>
  </channel>
</rss>

