<?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: How do I use Arrays and DO loop to create new variables? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694155#M25092</link>
    <description>&lt;P&gt;It isn't an "and" type question. You create new variables, one way at least, by declaring them in an array definition.&lt;/P&gt;
&lt;P&gt;A statement like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Array diff(8);&lt;/P&gt;
&lt;P&gt;will create 8 variables named diff1, diff2, and so forth.&lt;/P&gt;
&lt;P&gt;You would assign values to each indexed variable inside the do loop though.&lt;/P&gt;
&lt;P&gt;I would suggest NOT replacing the values in your weight variables so you can check results.&lt;/P&gt;
&lt;P&gt;It might make sense to create a second array of those though, so you are pretty close:&lt;/P&gt;
&lt;PRE&gt;DATA MiceMean;
	SET Work.Mice;
	MeanW= (MEAN(OF WtOz1-WtOz8));
	ARRAY wt{*} WtOz1 - WtOz8;
        array diff(8);
	DO i= 1 TO DIM(Diff);
	   Diff{i}= MeanW-wt{i};
	Drop i ;
	END;
RUN;&lt;/PRE&gt;
&lt;P&gt;It is very common to use parallel arrays. Just make sure that they all reference things in the expected order which can be tricky when using less than optimal names for variables.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Oct 2020 03:19:46 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-10-26T03:19:46Z</dc:date>
    <item>
      <title>How do I use Arrays and DO loop to create new variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694152#M25090</link>
      <description>&lt;P&gt;I'm struggling with a homework question for my SAS class. Using the provided data set "Mice" which contains the weight in ounces of 8 mice, I have to use arrays and a DO loop to create 8 new variables named "Diff1" to "Diff8" that contain the difference in weight between each mouse and the average weight of all 8 mice. I also have to calculate the average weight in the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the provided Data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.Mice;
	INFILE	DATALINES;
	INPUT	WtOz1 - WtOz8	@@;
	DATALINES;
0.57 0.69 0.87 0.86 0.83 0.63 0.68 0.59
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is my attempt at the question following an example problem. I think I got the right values but they appear as "WtOz1" and so on rather than "Diff1" and I know there's a better way than to just rename the variables. I also don't know how to calculate the average within the array.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MiceMean;
	SET Work.Mice;
	MeanW= (MEAN(OF WtOz1-WtOz8));
	ARRAY Diff{*} WtOz1 - WtOz8;
	DO i= 1 TO DIM(Diff);
	Diff{i}= MeanW-Diff{i};
	Drop i ;
	END;
	RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Amanda&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 03:01:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694152#M25090</guid>
      <dc:creator>abrice520</dc:creator>
      <dc:date>2020-10-26T03:01:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Arrays and DO loop to create new variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694154#M25091</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/344351"&gt;@abrice520&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Amanda,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Everything you're doing is basically correct.&amp;nbsp; You're saying MEAN which is the correct function, and you're using "of" which is the correct way to refer to multiple variables or an array.&amp;nbsp; You're using a variable range, WtOz1 - WtOz2, instead of an array reference.&amp;nbsp; An array reference for an array named WtOzs would be WtOzs[*].&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's one possible way to code this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.Mice;
	DROP	_:;
	INFILE	DATALINES;
	INPUT	WtOz1 - WtOz8	@@;

	ARRAY	WtOzs	[*]	WtOz1	-	WtOz8;
	ARRAY	DiffOzs	[*]	DiffOz1	-	DiffOz8;

	Avg_WtOz		=	MEAN(of	WtOzs[*]);

	DO	_i			=	1	TO	DIM(WtOzs);
		DiffOzs[_i]	=	Avg_Wtoz	-	WtOzs[_i];
	END;

DATALINES;
0.57 0.69 0.87 0.86 0.83 0.63 0.68 0.59
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Notice however that I have two arrays.&amp;nbsp; One array is for the values read in, the weights.&amp;nbsp; The other is for the calculation results, the differences.&amp;nbsp; I wouldn't try to do this in one array although you could.&amp;nbsp; If you don't want the weights in your results, then you could just code:&amp;nbsp; DROP WtOz1 - WtOz2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since my two arrays are essentially mirror images of one another, a single subscript, _i, can be used to access both.&amp;nbsp; The arrays function in parallel.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 03:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694154#M25091</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-26T03:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Arrays and DO loop to create new variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694155#M25092</link>
      <description>&lt;P&gt;It isn't an "and" type question. You create new variables, one way at least, by declaring them in an array definition.&lt;/P&gt;
&lt;P&gt;A statement like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Array diff(8);&lt;/P&gt;
&lt;P&gt;will create 8 variables named diff1, diff2, and so forth.&lt;/P&gt;
&lt;P&gt;You would assign values to each indexed variable inside the do loop though.&lt;/P&gt;
&lt;P&gt;I would suggest NOT replacing the values in your weight variables so you can check results.&lt;/P&gt;
&lt;P&gt;It might make sense to create a second array of those though, so you are pretty close:&lt;/P&gt;
&lt;PRE&gt;DATA MiceMean;
	SET Work.Mice;
	MeanW= (MEAN(OF WtOz1-WtOz8));
	ARRAY wt{*} WtOz1 - WtOz8;
        array diff(8);
	DO i= 1 TO DIM(Diff);
	   Diff{i}= MeanW-wt{i};
	Drop i ;
	END;
RUN;&lt;/PRE&gt;
&lt;P&gt;It is very common to use parallel arrays. Just make sure that they all reference things in the expected order which can be tricky when using less than optimal names for variables.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 03:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694155#M25092</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-26T03:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use Arrays and DO loop to create new variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694295#M25109</link>
      <description>Thank you so much!!</description>
      <pubDate>Mon, 26 Oct 2020 16:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-use-Arrays-and-DO-loop-to-create-new-variables/m-p/694295#M25109</guid>
      <dc:creator>abrice520</dc:creator>
      <dc:date>2020-10-26T16:32:21Z</dc:date>
    </item>
  </channel>
</rss>

