<?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 can I loop through observations and values in the dataset? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597862#M172306</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294893"&gt;@muskagap&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;When you code&amp;nbsp;&lt;SPAN&gt;Y_1950-Y_2000 in the array variable specification, you tell SAS to incorporate variables with the suffixes 1950 through 2000 &lt;EM&gt;inclusively&lt;/EM&gt;&amp;nbsp;incrementing them by 1. You cannot tell SAS to use a different increment. In your case, since all you variables are in the required sequence and all have the Y_ prefix, you can simply specify instead:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if you have other PDV variables prefixed with Y_, it's not going to work, and you'll have to list all the array variables explicitly, i.e.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 Y_1950 Y_1960 Y_1970 Y_1980 Y_1990 Y_2000 ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize that if you have, say, 100 variables like these, it can be quite inconvenient. To avoid such tedious hard coding, you can construct your variable list before your DATA step commences its compilation and pass it via a macro variable or via executing a macro. For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                                                                           
  call symputx ("varlist", "") ;                                                                                                        
  do i = 1950 to 2000 by 10 ;                                                                                                           
    call symputx ("varlist", catx (" ", symget ("varlist"), catx ("_", "Y", i))) ;                                                      
  end ;                                                                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data your_step ;                                                                                                                        
  array table1 &amp;amp;varlist ;                                                                                                               
run ;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you prefer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist (prefix, from, to, by) ;                                                                                                 
  %do i = &amp;amp;from %to &amp;amp;to %by &amp;amp;by ;                                                                                                       
    &amp;amp;prefix&amp;amp;i                                                                                                                           
  %end ;                                                                                                                                
%mend ;                                                                                                                                 
                                                                                                                                        
data _null_ ;                                                                                                                           
  array table1 %varlist(Y_,1950,2000,10) ;                                                                                              
run ;                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you're an SQL fan:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;                                                                                                                      
  select "Y_" || put (1950 + 10 * (monotonic()-1), z4.) into :varlist separated by " " from sashelp.zipcode where monotonic() le 6 ;    
quit ;                                                                                                                                  
                                                                                                                                        
data your_step ;                                                                                                                        
  array table1 &amp;amp;varlist ;                                                                                                               
run ;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NOTE, however: The array syntax above defines an &lt;EM&gt;implicitly subscripted&lt;/EM&gt; array indexed by the automatic auto-dropped variable _I_, so in your DO looping code you must use _I_ instead of INDEX. (The same is true about your DIFF array.) If you don't want to use it, there're two ways around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First: Tell SAS in the implicit array specification that you want the array indexed by INDEX:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 (index) Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second: Define an &lt;EM&gt;explicitly subscripted&lt;/EM&gt; array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array table1 [*] Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use any numeric variable you want for its index.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Sat, 19 Oct 2019 02:45:37 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-10-19T02:45:37Z</dc:date>
    <item>
      <title>How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597108#M172012</link>
      <description>&lt;DIV class="post-text"&gt;&lt;P&gt;I need to loop through a dataset (built-in, training SAS table with data) and do some calculations based on it. First off, I don't know how I could loop through values in range of columns of the table. Second, need to loop through all values in every observation and calculate difference between previous and following value. Here is the example of a table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Auto   Price1  Price2  Price3 
Mazda  35.000  40.500  38.000
Audi   70.000  60.000  80.000
BMW    110.000 85.000  93.000&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Let's say my table is named Prices in SAS. I tried below but I know it's totally wrong. I have no idea how to start:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data data.Prices;

set myDataset.Prices;
do price_prev = Price1;
    do price_next = Price2;

    end;
    end;

    result = price_next-price_prev;
 run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Desired output should be as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Auto   Diff1   Diff2    Diff3 
Mazda  0       5.500    -2.500
Audi   0       -10.000  20.000
BMW    0       -25.000  8.000&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need do use some kind of a loop but don't know how. Important thing is that I can't use macros or some other complex synthax. Just loops.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 16 Oct 2019 21:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597108#M172012</guid>
      <dc:creator>muskagap</dc:creator>
      <dc:date>2019-10-16T21:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597113#M172013</link>
      <description>&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;P&gt;&lt;STRONG&gt;SAS loops through each row automatically in a data step.&lt;/STRONG&gt; If you don't understand how a data step works, that may the first thing to start with.&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=basess&amp;amp;docsetTarget=n053a58fwk57v7n14h8x7y7u34y4.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=basess&amp;amp;docsetTarget=n053a58fwk57v7n14h8x7y7u34y4.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&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/294893"&gt;@muskagap&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;DIV class="post-text"&gt;
&lt;P&gt;I need to loop through a dataset (built-in, training SAS table with data) and do some calculations based on it. First off, I don't know how I could loop through values in range of columns of the table. Second, need to loop through all values in every observation and calculate difference between previous and following value. Here is the example of a table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Auto   Price1  Price2  Price3 
Mazda  35.000  40.500  38.000
Audi   70.000  60.000  80.000
BMW    110.000 85.000  93.000&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's say my table is named Prices in SAS. I tried below but I know it's totally wrong. I have no idea how to start:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;data data.Prices;

set myDataset.Prices;
do price_prev = Price1;
    do price_next = Price2;

    end;
    end;

    result = price_next-price_prev;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Desired output should be as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Auto   Diff1   Diff2    Diff3 
Mazda  0       5.500    -2.500
Audi   0       -10.000  20.000
BMW    0       -25.000  8.000&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I need do use some kind of a loop but don't know how. Important thing is that I can't use macros or some other complex synthax. Just loops.&lt;/P&gt;
&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 21:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597113#M172013</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-16T21:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597117#M172015</link>
      <description>&lt;P&gt;Ok , tahnks. Need to switch from other programming languages to SAS, it works differently.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 21:47:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597117#M172015</guid>
      <dc:creator>muskagap</dc:creator>
      <dc:date>2019-10-16T21:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597123#M172017</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294893"&gt;@muskagap&lt;/a&gt;&amp;nbsp; One way to learn though may sound slow is to actually try and do without loops, and then understand what's going on to make it iterative leading to loops and key indexing. So I would suggest without loops first. Welcome to SAS communities&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Auto $  (Price1  Price2  Price3) (:comma10.) ;
format price: comma10.;
cards;
Mazda  35,000  40,500  38,000
Audi   70,000  60,000  80,000
BMW    110,000 85,000  93,000
;

data want;
set have;
Diff1=0;
Diff2=Price2-Price1;
Diff3=Price3-Price2;
format diff: comma10.;
run;
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2019 21:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597123#M172017</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-16T21:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597131#M172020</link>
      <description>Yeah, if you generally think of SAS processing a single row at a time that'll help a lot.</description>
      <pubDate>Wed, 16 Oct 2019 22:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597131#M172020</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-16T22:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597822#M172293</link>
      <description>&lt;P&gt;I have one more question: need to loop through columns (variables)&amp;nbsp; and calculate the difference between next and prior value. The table is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;         Y_1950     Y_1960     Y_1970     Y_1980     Y_1990     Y_2000
          3281       3413       3114       2500       2700       3500
          4042       3084       3108       3150       3800       3100
          6015       6123       6113       6100       6100       6200&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created 2 arrays and wrote such a code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data loopTables;&lt;/P&gt;&lt;P&gt;set sashelp.us_data;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ARRAY table1 Y_1950-Y_2000;&lt;BR /&gt;ARRAY diff diff1-diff5;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;diff[1]=0;&lt;/P&gt;&lt;P&gt;do index = 2 to DIM(table1);&lt;BR /&gt;diff[index] = table1[index]-table1[index-1];&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately it doesn't work. I have 2 questions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. How can I associate columns names in array if their suffixes increase by 10? (1950, 1960 etc.)? I noticed that 1st array itself returns&lt;/P&gt;&lt;P&gt;Y_1950,Y_1951,Y_1952,Y_1953,Y_1954, etc. I need it to return variables as in my origin table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Second array: if my first array returns Y_1950-Y_2000 how I should associate 'diff' with first array to return values from corresponding columns from first array? Sth like diff_1950-diff_2000 or diff_1-diff_5?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would be grateful for any explanation.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 20:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597822#M172293</guid>
      <dc:creator>muskagap</dc:creator>
      <dc:date>2019-10-18T20:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through observations and values in the dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597862#M172306</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294893"&gt;@muskagap&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;When you code&amp;nbsp;&lt;SPAN&gt;Y_1950-Y_2000 in the array variable specification, you tell SAS to incorporate variables with the suffixes 1950 through 2000 &lt;EM&gt;inclusively&lt;/EM&gt;&amp;nbsp;incrementing them by 1. You cannot tell SAS to use a different increment. In your case, since all you variables are in the required sequence and all have the Y_ prefix, you can simply specify instead:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if you have other PDV variables prefixed with Y_, it's not going to work, and you'll have to list all the array variables explicitly, i.e.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 Y_1950 Y_1960 Y_1970 Y_1980 Y_1990 Y_2000 ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize that if you have, say, 100 variables like these, it can be quite inconvenient. To avoid such tedious hard coding, you can construct your variable list before your DATA step commences its compilation and pass it via a macro variable or via executing a macro. For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                                                                           
  call symputx ("varlist", "") ;                                                                                                        
  do i = 1950 to 2000 by 10 ;                                                                                                           
    call symputx ("varlist", catx (" ", symget ("varlist"), catx ("_", "Y", i))) ;                                                      
  end ;                                                                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data your_step ;                                                                                                                        
  array table1 &amp;amp;varlist ;                                                                                                               
run ;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you prefer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist (prefix, from, to, by) ;                                                                                                 
  %do i = &amp;amp;from %to &amp;amp;to %by &amp;amp;by ;                                                                                                       
    &amp;amp;prefix&amp;amp;i                                                                                                                           
  %end ;                                                                                                                                
%mend ;                                                                                                                                 
                                                                                                                                        
data _null_ ;                                                                                                                           
  array table1 %varlist(Y_,1950,2000,10) ;                                                                                              
run ;                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you're an SQL fan:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;                                                                                                                      
  select "Y_" || put (1950 + 10 * (monotonic()-1), z4.) into :varlist separated by " " from sashelp.zipcode where monotonic() le 6 ;    
quit ;                                                                                                                                  
                                                                                                                                        
data your_step ;                                                                                                                        
  array table1 &amp;amp;varlist ;                                                                                                               
run ;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NOTE, however: The array syntax above defines an &lt;EM&gt;implicitly subscripted&lt;/EM&gt; array indexed by the automatic auto-dropped variable _I_, so in your DO looping code you must use _I_ instead of INDEX. (The same is true about your DIFF array.) If you don't want to use it, there're two ways around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First: Tell SAS in the implicit array specification that you want the array indexed by INDEX:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array table1 (index) Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second: Define an &lt;EM&gt;explicitly subscripted&lt;/EM&gt; array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array table1 [*] Y_: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use any numeric variable you want for its index.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2019 02:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-loop-through-observations-and-values-in-the-dataset/m-p/597862#M172306</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-19T02:45:37Z</dc:date>
    </item>
  </channel>
</rss>

