<?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 subsetting data and copying values based on conditions using loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932874#M366945</link>
    <description>&lt;P&gt;I'm working on a SAS program that's long and was wondering if I can convert the following codes into loops so that it's easier for others to read and review/check. (I changed to generic variables for simplicity.)&lt;/P&gt;&lt;P&gt;I'm also learning loops so trying to use it as much as possible to get some practice.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this section, I subset the data into different datasets based on the value of a variable.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data
	Locations1 
	Locations2
	Locations3
	Locations4  
	Locations5
	Locations6
	Locations7
	Locations8
	Locations9
	Locations10;
set data_all; 
if number_of_addresses = 1  then output Locations1;  
if number_of_addresses = 2  then output Locations2;  
if number_of_addresses = 3  then output Locations3; 
if number_of_addresses = 4  then output Locations4; 
if number_of_addresses = 5  then output Locations5; 
if number_of_addresses = 6  then output Locations6; 
if number_of_addresses = 7  then output Locations7; 
if number_of_addresses = 8  then output Locations8; 
if number_of_addresses = 9  then output Locations9; 
if number_of_addresses = 10 then output Locations10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV&gt;In this section, I'm calculating percentage based on the value of a variable.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data percent;
set data_all; &lt;BR /&gt;if A &amp;lt;= 2 then percent_2 = num_employees / totals2; 
if A &amp;lt;= 3 then percent_3 = num_employees / totals3; 
if A &amp;lt;= 4 then percent_4 = num_employees / totals4; 
if A &amp;lt;= 5 then percent_5 = num_employees / totals5; 
if A &amp;lt;= 6 then percent_6 = num_employees / totals6; 
if A &amp;lt;= 7 then percent_7 = num_employees / totals7; 
if A &amp;lt;= 8 then percent_8 = num_employees / totals8; 
if A &amp;lt;= 9 then percent_9 = num_employees / totals9; 
if A &amp;lt;= 10 then percent_10 = num_employees / totals10; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;Finally, here I'm just copying values from multiple variables into one variable based on the value of a variable.&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data visit;
set data_all; 
if G = 2 then V = V_all2; 
if G = 3 then V = V_all3; 
if G = 4 then V = V_all4; 
if G = 5 then V = V_all5; 
if G = 6 then V = V_all6; 
if G = 7 then V = V_all7; 
if G = 8 then V = V_all8; 
if G = 9 then V = V_all9; 
if G = 10 then V = V_all10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;Thanks in advance!&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Tue, 18 Jun 2024 16:11:35 GMT</pubDate>
    <dc:creator>wfung</dc:creator>
    <dc:date>2024-06-18T16:11:35Z</dc:date>
    <item>
      <title>subsetting data and copying values based on conditions using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932874#M366945</link>
      <description>&lt;P&gt;I'm working on a SAS program that's long and was wondering if I can convert the following codes into loops so that it's easier for others to read and review/check. (I changed to generic variables for simplicity.)&lt;/P&gt;&lt;P&gt;I'm also learning loops so trying to use it as much as possible to get some practice.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this section, I subset the data into different datasets based on the value of a variable.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data
	Locations1 
	Locations2
	Locations3
	Locations4  
	Locations5
	Locations6
	Locations7
	Locations8
	Locations9
	Locations10;
set data_all; 
if number_of_addresses = 1  then output Locations1;  
if number_of_addresses = 2  then output Locations2;  
if number_of_addresses = 3  then output Locations3; 
if number_of_addresses = 4  then output Locations4; 
if number_of_addresses = 5  then output Locations5; 
if number_of_addresses = 6  then output Locations6; 
if number_of_addresses = 7  then output Locations7; 
if number_of_addresses = 8  then output Locations8; 
if number_of_addresses = 9  then output Locations9; 
if number_of_addresses = 10 then output Locations10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV&gt;In this section, I'm calculating percentage based on the value of a variable.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data percent;
set data_all; &lt;BR /&gt;if A &amp;lt;= 2 then percent_2 = num_employees / totals2; 
if A &amp;lt;= 3 then percent_3 = num_employees / totals3; 
if A &amp;lt;= 4 then percent_4 = num_employees / totals4; 
if A &amp;lt;= 5 then percent_5 = num_employees / totals5; 
if A &amp;lt;= 6 then percent_6 = num_employees / totals6; 
if A &amp;lt;= 7 then percent_7 = num_employees / totals7; 
if A &amp;lt;= 8 then percent_8 = num_employees / totals8; 
if A &amp;lt;= 9 then percent_9 = num_employees / totals9; 
if A &amp;lt;= 10 then percent_10 = num_employees / totals10; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;Finally, here I'm just copying values from multiple variables into one variable based on the value of a variable.&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data visit;
set data_all; 
if G = 2 then V = V_all2; 
if G = 3 then V = V_all3; 
if G = 4 then V = V_all4; 
if G = 5 then V = V_all5; 
if G = 6 then V = V_all6; 
if G = 7 then V = V_all7; 
if G = 8 then V = V_all8; 
if G = 9 then V = V_all9; 
if G = 10 then V = V_all10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;Thanks in advance!&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 18 Jun 2024 16:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932874#M366945</guid>
      <dc:creator>wfung</dc:creator>
      <dc:date>2024-06-18T16:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: subsetting data and copying values based on conditions using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932877#M366947</link>
      <description>&lt;P&gt;Some comments:&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Splitting data into separate data sets is usually something to avoid. It is a poor practice that is inefficient and makes your coding a lot more tedious. Even if you could do this with loops, one of the things you need to understand is when to split the data and when not to split the data, and I wouldn't bother learning loops here.&lt;/LI&gt;
&lt;LI&gt;Your second block of code seems like a simple array.&lt;/LI&gt;
&lt;LI&gt;Your third block of code seems like a simple array.&lt;/LI&gt;
&lt;LI&gt;However, these examples seem unrealistic to me, that I have never done anything like this in real life. It would be helpful to for you to find more realistic data sets and examples.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 18 Jun 2024 16:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932877#M366947</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-18T16:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: subsetting data and copying values based on conditions using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932880#M366948</link>
      <description>&lt;P&gt;Here's a more realistic example where loops might be necessary. Data set&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/statug/statug_code_plsex3.htm" target="_self"&gt; here&lt;/A&gt;. Suppose you want to find the number of times in the variables F1-F30 where F(i) &amp;gt; F(i-1) and the number of times where F(i) is 10% greater (or more) than F(i-1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set ftest;
    array f f1-f30;
    greater_than=0;
    ten_percent_greater=0;
    do i=2 to dim(f);
         if f(i)&amp;gt;f(i-1) then greater_than = greater_than+1;
         if f(i)&amp;gt;=1.1*f(i-1) then ten_percent_greater=ten_percent_greater+1;
    end;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 16:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932880#M366948</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-18T16:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: subsetting data and copying values based on conditions using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932881#M366949</link>
      <description>&lt;P&gt;For #1 pretty much follow this example&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/4bfb7574713bedf4e011" target="_blank"&gt;https://gist.github.com/statgeek/4bfb7574713bedf4e011&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Sort data set by splitting variable&lt;/LI&gt;
&lt;LI&gt;Generate data step code to split file&lt;/LI&gt;
&lt;LI&gt;CALL Execute splits file&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	proc sort data=data_all;
	by num_of_addresses;
	run;

	data _null_;
	set data_all;
	by num_of_addresses;

	if first.num_of_addresses then do;


		*create string;
		str1='data LOCATIONS'||put(num_of_addresses ,z3.)||"; set data_all; where num_of_addresses ="||put(num_of_addresses , 2.)||";run;";
		call execute(str1);

	end;
	run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;For #2/3 use arrays.&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data percent;
set data_all;

array _per(2:10) percent_2-percent_10;
array _tot(2:10) totals2-totals10;
array _v(2:10) v_all2-v_all10;

do i=2 to 10;
  _per(i) = num_employees/_tot(i);

if G=i then V = _v(i);
   
end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129586"&gt;@wfung&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm working on a SAS program that's long and was wondering if I can convert the following codes into loops so that it's easier for others to read and review/check. (I changed to generic variables for simplicity.)&lt;/P&gt;
&lt;P&gt;I'm also learning loops so trying to use it as much as possible to get some practice.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this section, I subset the data into different datasets based on the value of a variable.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data
	Locations1 
	Locations2
	Locations3
	Locations4  
	Locations5
	Locations6
	Locations7
	Locations8
	Locations9
	Locations10;
set data_all; 
if number_of_addresses = 1  then output Locations1;  
if number_of_addresses = 2  then output Locations2;  
if number_of_addresses = 3  then output Locations3; 
if number_of_addresses = 4  then output Locations4; 
if number_of_addresses = 5  then output Locations5; 
if number_of_addresses = 6  then output Locations6; 
if number_of_addresses = 7  then output Locations7; 
if number_of_addresses = 8  then output Locations8; 
if number_of_addresses = 9  then output Locations9; 
if number_of_addresses = 10 then output Locations10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;In this section, I'm calculating percentage based on the value of a variable.&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data percent;
set data_all; &lt;BR /&gt;if A &amp;lt;= 2 then percent_2 = num_employees / totals2; 
if A &amp;lt;= 3 then percent_3 = num_employees / totals3; 
if A &amp;lt;= 4 then percent_4 = num_employees / totals4; 
if A &amp;lt;= 5 then percent_5 = num_employees / totals5; 
if A &amp;lt;= 6 then percent_6 = num_employees / totals6; 
if A &amp;lt;= 7 then percent_7 = num_employees / totals7; 
if A &amp;lt;= 8 then percent_8 = num_employees / totals8; 
if A &amp;lt;= 9 then percent_9 = num_employees / totals9; 
if A &amp;lt;= 10 then percent_10 = num_employees / totals10; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;Finally, here I'm just copying values from multiple variables into one variable based on the value of a variable.&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data visit;
set data_all; 
if G = 2 then V = V_all2; 
if G = 3 then V = V_all3; 
if G = 4 then V = V_all4; 
if G = 5 then V = V_all5; 
if G = 6 then V = V_all6; 
if G = 7 then V = V_all7; 
if G = 8 then V = V_all8; 
if G = 9 then V = V_all9; 
if G = 10 then V = V_all10; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
Thanks in advance!&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 16:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/subsetting-data-and-copying-values-based-on-conditions-using/m-p/932881#M366949</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-06-18T16:53:07Z</dc:date>
    </item>
  </channel>
</rss>

