<?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: Get code to work in an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788818#M252317</link>
    <description>&lt;P&gt;Please show the expected result.&lt;/P&gt;
&lt;P&gt;You may need to arrays, one for the values in the current observation, another one for the retained variables, both need variable-names, _numeric_ won't work.&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jan 2022 10:30:59 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-01-07T10:30:59Z</dc:date>
    <item>
      <title>Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788817#M252316</link>
      <description>&lt;P&gt;Dear SAS experts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have some code which I cannot get to work. I want to use the data points for each numeric variable in the first observation and divide by this number in the data points below the first observation. I can get it to work if I take one variable at a time, but I cannot get the code with an array to work. Specifically, I cannot seem to define the variable which is retained and used in the simple calculation (see &lt;STRONG&gt;?????&lt;/STRONG&gt;). Does anyone have a suggestion on how get the code to work using an array? Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input varone vartwo varthree varfour;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 1.5 5&lt;BR /&gt;1 2 1 1&lt;BR /&gt;1 3 1 2&lt;BR /&gt;1 4 3 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;set have;&lt;BR /&gt;globalcatvar="1";&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*No array;&lt;BR /&gt;data want_three;&lt;BR /&gt;set have;&lt;BR /&gt;by globalcatvar;&lt;BR /&gt;retain firstvartwo; &lt;BR /&gt;if first.globalcatvar then firstvartwo=vartwo;&lt;BR /&gt;if _n_&amp;gt;1 then vartwo=vartwo/firstvartwo;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*With array;&lt;BR /&gt;data want_three;&lt;BR /&gt;set have;&lt;BR /&gt;array modify {*} _NUMERIC_;&lt;BR /&gt;do i=1 to dim(modify);&lt;BR /&gt;by globalcatvar;&lt;BR /&gt;retain &lt;STRONG&gt;?????&lt;/STRONG&gt;;&lt;BR /&gt;if first.globalcatvar then &lt;STRONG&gt;?????&lt;/STRONG&gt;=modify{i};&lt;BR /&gt;if _n_&amp;gt;1 then modify{i}=modify{i}/&lt;STRONG&gt;?????&lt;/STRONG&gt;;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 10:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788817#M252316</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2022-01-07T10:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788818#M252317</link>
      <description>&lt;P&gt;Please show the expected result.&lt;/P&gt;
&lt;P&gt;You may need to arrays, one for the values in the current observation, another one for the retained variables, both need variable-names, _numeric_ won't work.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 10:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788818#M252317</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-07T10:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788820#M252319</link>
      <description>&lt;P&gt;Hey Andreas&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;P&gt;For vartwo, I would like it to make the following change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2 = 2 (no change)&lt;/P&gt;
&lt;P&gt;2 = 2/2 = 1&lt;/P&gt;
&lt;P&gt;3 = 3/2 = 1.5&lt;/P&gt;
&lt;P&gt;4 = 4/2 = 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Specifically, each number is divided by the first data point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So some code which looks something like this (although the array does not work)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input varone vartwo varthree varfour;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 1.5 5&lt;BR /&gt;1 2 1 1&lt;BR /&gt;1 3 1 2&lt;BR /&gt;1 4 3 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;set have;&lt;BR /&gt;globalcatvar="1";&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Array:&lt;BR /&gt;data want_three;&lt;BR /&gt;set have;&lt;BR /&gt;array modify {4} varone vartwo varthree varfour;&lt;BR /&gt;array modify_r {4} varone_r vartwo_r varthree_r varfour_r;&lt;BR /&gt;do i=1 to 4;&lt;BR /&gt;by globalcatvar;&lt;BR /&gt;retain modify_r{i};&lt;BR /&gt;if first.globalcatvar then modify_r{i}=modify{i};&lt;BR /&gt;if _n_&amp;gt;1 then modify{i}=modify{i}/modify_r{i};&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 11:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788820#M252319</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2022-01-07T11:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788828#M252324</link>
      <description>&lt;P&gt;I think the right approach is to use a _TEMPORARY_ array, these are automatically retained:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_three;
set have;
by globalcatvar;
array modify {*} _NUMERIC_;
array retains (200) 8 _temporary_;
if first.globalcatvar  then do i=1 to dim(modify);
  retains(i)=modify(i);
  end;
else do i=1 to dim(modify);
  modify{i}=modify{i}/retains(i);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:00:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788828#M252324</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-07T12:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788835#M252328</link>
      <description>&lt;P&gt;Hey s_lassen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code looks very cool. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But what is the signifiance of (200) and 8 specified in the array 'retains'? Is 200 just a large number (could e.g. also have been 400) ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788835#M252328</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2022-01-07T12:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788837#M252330</link>
      <description>&lt;P&gt;Still not clear, why globalcatvar is defined ... the following step is build on some assumptions:&lt;/P&gt;
&lt;P&gt;- you want to divide varthree and varfour also&lt;/P&gt;
&lt;P&gt;- varone is constant&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   
   array vars[3] vartwo varthree varfour;
   array re[3] _temporary_;
   
   if _n_ = 1 then do;
      do i = 1 to dim(re);
         re[i] = vars[i];
      end;
   end;
   else do;
      do i = 1 to dim(re);
         vars[i] = vars[i] / re[i];
      end;      
   end;
   
   drop i;
run;
   &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788837#M252330</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-07T12:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788841#M252334</link>
      <description>&lt;P&gt;200 is just a number that I assume is larger than the dimension of of the other array, but not so large that it takes up all available memory. There is probably no problem in increasing it to e.g. 2000 if you sometimes have that many numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;8 is just the length of the array elements, as there is no dollar sign, it defines the _TEMPORARY_ array as numeric.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788841#M252334</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-07T12:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788843#M252336</link>
      <description>&lt;P&gt;Hey Andreas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks. No, I might not need globalcatvar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code looks interesting. Is it possible to modify it such that all numeric variables are included in vars (and that the rest of the code can be modified to work too)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788843#M252336</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2022-01-07T12:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788846#M252338</link>
      <description>&lt;P&gt;By "dimension" do you mean number of elements in the array?&lt;/P&gt;
&lt;P&gt;So the "8" says something about how large the numbers included in the array retains are expected to be?&lt;/P&gt;
&lt;P&gt;I am relatively new to SAS coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I slightly changed some of your code. I removed the the by-code and the globalcatvar, which I thought was neccessary. The code seems to be doing what I was hoping for. Moreover, it does appear that the code below would work without any knowledge about the number of numeric variables in the dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input varone vartwo varthree varfour;&lt;BR /&gt;datalines;&lt;BR /&gt;1.1 2 1.5 5&lt;BR /&gt;1 2 1 1&lt;BR /&gt;1 3 1 2&lt;BR /&gt;1 4 3 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want_three;&lt;BR /&gt;set have;&lt;BR /&gt;array modify {*} _NUMERIC_;&lt;BR /&gt;array retains (200) 8 _temporary_;&lt;BR /&gt;if &lt;STRONG&gt;_n_=1&lt;/STRONG&gt; then do i=1 to dim(modify);&lt;BR /&gt;retains(i)=modify(i);&lt;BR /&gt;end;&lt;BR /&gt;else do i=1 to dim(modify);&lt;BR /&gt;modify{i}=modify{i}/retains(i);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 12:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788846#M252338</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2022-01-07T12:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get code to work in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788859#M252344</link>
      <description>&lt;P&gt;untested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select num_numeric into :numVars trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = 'HAVE'
  ;
quit;

data want;
   set have;
   
   array vars _numeric_; 
   array re[&amp;amp;numVars.] _temporary_;
   
   if _n_ = 1 then do;
      do i = 1 to dim(re);
         re[i] = vars[i];
      end;
   end;
   else do;
      do i = 1 to dim(re);
         vars[i] = vars[i] / re[i];
      end;      
   end;
   
   drop i;
run;
   


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jan 2022 13:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-code-to-work-in-an-array/m-p/788859#M252344</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-07T13:37:44Z</dc:date>
    </item>
  </channel>
</rss>

