<?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: Use Do Loop to Create Fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546729#M151431</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;I think the OP has 144 observations, not 144 variables....though it's possible they have a data set with 288+ columns.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Given that you have 144 sets of variables:&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 count_ {144};
   array region_ {144};
   array tract_ {144};
   do i=1 to 144;
      count_{i} = coalesce(region_{i}, tract_{i});
   end;
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Mar 2019 21:37:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-03-27T21:37:12Z</dc:date>
    <item>
      <title>Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546679#M151398</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set of rabbit counts by region over several years. The only problem is, the data set has two sets of fields for each region, and only one of those fields is populated. I am trying to collapse the fields so that I only have one field for rabbit count. I am using the coalesce function for this. For example, if Region_1 count = 5, then the corresponding Tract_1 count will be blank. If Tract_2 count=1,733, then the corresponding Region_2 count will be blank. However, SAS does not seem to accept using the counter variable as part of the field name. Any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data rabbit_reformat;
set rabbits ;
do i=1 to 144;
Count_&amp;amp;i. =coalesce(Region_&amp;amp;i.,Tract_&amp;amp;i.);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to reformat the table, NOT sum it up at this point.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 20:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546679#M151398</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2019-03-27T20:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546683#M151400</link>
      <description>You need to explainhow your data is structured and what you expect as output. Without that, no way to know if your code is correct. and you cannot use &amp;amp;i in the manner you are. You can use arrays if needed, but I suspect you don't.</description>
      <pubDate>Wed, 27 Mar 2019 20:37:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546683#M151400</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-27T20:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546697#M151407</link>
      <description>&lt;P&gt;This is what I have:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="2"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;State&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;Region_1&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;Tract_1&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;Region_2&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;Tract_2&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;AZ&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;Five&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;One Thousand Seven Hundred and Thirty Three&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;CA&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;Eight&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;Two Thousand and Three&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and this is what I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="2"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;State&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;Rabbits_1&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;Rabbits_2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;AZ&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;Five&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;One Thousand Seven Hundred and Thirty Three&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="left"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;CA&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;Eight&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="right"&gt;&lt;FONT face="Calibri" size="3"&gt;&lt;FONT face="Calibri" size="3"&gt;Two Thousand and Three&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&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, 27 Mar 2019 20:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546697#M151407</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2019-03-27T20:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546704#M151414</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input State $	Region_1	Tract_1	Region_2	Tract_2;
cards;
AZ	5		.	1733 . .
CA	.	8	2158 . .
;	

data temp;
set have;
array t Region_1--Tract_2;
do i=1 to dim(t);
if t(i)&amp;gt;. then do;
temp=t(i);
output;
end;
end;
run;

proc transpose data=temp out=want(drop=_:) prefix=Rabbit_;
by state;
var temp;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Mar 2019 21:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546704#M151414</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-03-27T21:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546709#M151419</link>
      <description>No loops or anything else needed then, SAS loops each automatically. &lt;BR /&gt;&lt;BR /&gt;rabbits_1 = coalesce(region_1, tract_1);&lt;BR /&gt;rabbits_2 = coalesce(region_2, tract_2);&lt;BR /&gt;&lt;BR /&gt;If this needed to be generalized beyond two cases then an array would be the solution.</description>
      <pubDate>Wed, 27 Mar 2019 21:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546709#M151419</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-27T21:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546719#M151427</link>
      <description>&lt;P&gt;Given that you have 144 sets of variables:&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 count_ {144};
   array region_ {144};
   array tract_ {144};
   do i=1 to 144;
      count_{i} = coalesce(region_{i}, tract_{i});
   end;
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Mar 2019 21:17:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546719#M151427</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-27T21:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546726#M151430</link>
      <description>&lt;P&gt;Transpose, and you don't need arrays or dynamic code.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 21:30:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546726#M151430</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-27T21:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: Use Do Loop to Create Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546729#M151431</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;I think the OP has 144 observations, not 144 variables....though it's possible they have a data set with 288+ columns.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Given that you have 144 sets of variables:&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 count_ {144};
   array region_ {144};
   array tract_ {144};
   do i=1 to 144;
      count_{i} = coalesce(region_{i}, tract_{i});
   end;
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 21:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Do-Loop-to-Create-Fields/m-p/546729#M151431</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-27T21:37:12Z</dc:date>
    </item>
  </channel>
</rss>

