<?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 Use temporary array/do loop to check the value of one variable/one observation against many values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551240#M153146</link>
    <description>&lt;P&gt;Hi all! Hoping for some help or new ideas ....I am trying to test whether the value of a variable for one observation is greater than the value of another variable for any observation in my table. See code and log below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*First, I add this list of values from Column1 into a new macrovariable varlist, separated by a space.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;%let varlist = ;&lt;BR /&gt;select Column1 into :varlist separated by ' '&lt;BR /&gt;from have;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Then, I add the count of all the values added into the previous macro into a new macrovariable a. I could just check the log/length of my table but I'd like this to be reusable for other datasets and I'm too lazy to look every time.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;%let a = ;&lt;BR /&gt;select count(Column1) into :a&lt;BR /&gt;from have;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Then, I add the macro variables from above into a SAS temporary array and attempt to use a do loop to look for, and flag, rows where the value of Column 2 is greater than the value of Column 1. I want to know if Column 2 is greater than Column 1 anywhere in the table, not just for the observation it's listed next to.*/&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array variables (&amp;amp;a.) &amp;amp;varlist.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i = 1 to &amp;amp;a.;&lt;BR /&gt;&lt;BR /&gt;if variables(i) &amp;gt; Column2 then flag = 1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here's my log output when I attempt to run this:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;314 proc sql;&lt;BR /&gt;315 %let varlist = ;&lt;BR /&gt;316 select Column1 into :varlist separated by ' '&lt;BR /&gt;317 from have;&lt;BR /&gt;318 quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.04 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;319&lt;BR /&gt;320 proc sql;&lt;BR /&gt;321 %let a = ;&lt;BR /&gt;322 select count(Column1) into :a&lt;BR /&gt;323 from have;&lt;BR /&gt;324 quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.02 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;325&lt;BR /&gt;326 data want;&lt;BR /&gt;327 set have;&lt;BR /&gt;328 array variables (&amp;amp;a.) &amp;amp;varlist.;&lt;BR /&gt;-&lt;BR /&gt;79&lt;BR /&gt;ERROR 79-322: Expecting a ).&lt;/P&gt;&lt;P&gt;NOTE: Line generated by the macro variable "VARLIST".&lt;BR /&gt;1 3679 4455 4179 3747 2113 4168 4183 3157 3240 4165 5252 3981 3788 2079 3079 2867 3840 4638&lt;BR /&gt;---- ----&lt;BR /&gt;352 79&lt;BR /&gt;1 ! 1719 2912 5558 4679 4328 4134 2587 2709 2258 3923 4377 2873 2290 5172 4699 2541 3466 3496&lt;BR /&gt;1 ! 3966 3041 3728 4693 4649 2274 3012 4551 3955 4958 3021 3592 1479 3684 4698&lt;BR /&gt;ERROR 352-185: The length of numeric variables is 3-8.&lt;/P&gt;&lt;P&gt;ERROR 79-322: Expecting a (.&lt;/P&gt;&lt;P&gt;329&lt;BR /&gt;330 do i = 1 to 77;&lt;BR /&gt;WARNING: Partial value initialization of the array variables.&lt;BR /&gt;331&lt;BR /&gt;332 if variables(i) &amp;gt; Column2 then flag = 1;&lt;BR /&gt;333&lt;BR /&gt;334 end;&lt;BR /&gt;335&lt;BR /&gt;336 run;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0&lt;BR /&gt;observations and 90 variables.&lt;BR /&gt;WARNING: Data set WORK.TEST was not replaced because this step was stopped.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.04 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;______&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like this is pretty inefficient and also...it's not working! Thanks in advance for any insight you can provide!&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2019 20:14:40 GMT</pubDate>
    <dc:creator>clambert22</dc:creator>
    <dc:date>2019-04-15T20:14:40Z</dc:date>
    <item>
      <title>Use temporary array/do loop to check the value of one variable/one observation against many values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551240#M153146</link>
      <description>&lt;P&gt;Hi all! Hoping for some help or new ideas ....I am trying to test whether the value of a variable for one observation is greater than the value of another variable for any observation in my table. See code and log below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*First, I add this list of values from Column1 into a new macrovariable varlist, separated by a space.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;%let varlist = ;&lt;BR /&gt;select Column1 into :varlist separated by ' '&lt;BR /&gt;from have;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Then, I add the count of all the values added into the previous macro into a new macrovariable a. I could just check the log/length of my table but I'd like this to be reusable for other datasets and I'm too lazy to look every time.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;%let a = ;&lt;BR /&gt;select count(Column1) into :a&lt;BR /&gt;from have;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Then, I add the macro variables from above into a SAS temporary array and attempt to use a do loop to look for, and flag, rows where the value of Column 2 is greater than the value of Column 1. I want to know if Column 2 is greater than Column 1 anywhere in the table, not just for the observation it's listed next to.*/&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array variables (&amp;amp;a.) &amp;amp;varlist.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i = 1 to &amp;amp;a.;&lt;BR /&gt;&lt;BR /&gt;if variables(i) &amp;gt; Column2 then flag = 1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here's my log output when I attempt to run this:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;314 proc sql;&lt;BR /&gt;315 %let varlist = ;&lt;BR /&gt;316 select Column1 into :varlist separated by ' '&lt;BR /&gt;317 from have;&lt;BR /&gt;318 quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.04 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;319&lt;BR /&gt;320 proc sql;&lt;BR /&gt;321 %let a = ;&lt;BR /&gt;322 select count(Column1) into :a&lt;BR /&gt;323 from have;&lt;BR /&gt;324 quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.02 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;325&lt;BR /&gt;326 data want;&lt;BR /&gt;327 set have;&lt;BR /&gt;328 array variables (&amp;amp;a.) &amp;amp;varlist.;&lt;BR /&gt;-&lt;BR /&gt;79&lt;BR /&gt;ERROR 79-322: Expecting a ).&lt;/P&gt;&lt;P&gt;NOTE: Line generated by the macro variable "VARLIST".&lt;BR /&gt;1 3679 4455 4179 3747 2113 4168 4183 3157 3240 4165 5252 3981 3788 2079 3079 2867 3840 4638&lt;BR /&gt;---- ----&lt;BR /&gt;352 79&lt;BR /&gt;1 ! 1719 2912 5558 4679 4328 4134 2587 2709 2258 3923 4377 2873 2290 5172 4699 2541 3466 3496&lt;BR /&gt;1 ! 3966 3041 3728 4693 4649 2274 3012 4551 3955 4958 3021 3592 1479 3684 4698&lt;BR /&gt;ERROR 352-185: The length of numeric variables is 3-8.&lt;/P&gt;&lt;P&gt;ERROR 79-322: Expecting a (.&lt;/P&gt;&lt;P&gt;329&lt;BR /&gt;330 do i = 1 to 77;&lt;BR /&gt;WARNING: Partial value initialization of the array variables.&lt;BR /&gt;331&lt;BR /&gt;332 if variables(i) &amp;gt; Column2 then flag = 1;&lt;BR /&gt;333&lt;BR /&gt;334 end;&lt;BR /&gt;335&lt;BR /&gt;336 run;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0&lt;BR /&gt;observations and 90 variables.&lt;BR /&gt;WARNING: Data set WORK.TEST was not replaced because this step was stopped.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.04 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;______&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like this is pretty inefficient and also...it's not working! Thanks in advance for any insight you can provide!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 20:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551240#M153146</guid>
      <dc:creator>clambert22</dc:creator>
      <dc:date>2019-04-15T20:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use temporary array/do loop to check the value of one variable/one observation against many valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551247#M153150</link>
      <description>&lt;P&gt;Yes, the programming has gotten a little out of hand here.&amp;nbsp; Try it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select max(column2) into : maxval from have;
quit;

data want;
   set have;
   flag = (column1 &amp;gt; &amp;amp;maxval);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not sure if I got the interpretation correct, or whether you might have to switch around the roles of column1 and column2 here&amp;nbsp; But the final program looks like it should be this simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;********* EDITED:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Maybe this is closer to what you want?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select max(column1) into : maxval from have;
run;

data want;
   flag=0;
   do until (done or flag=1);
      set have;
      if column2 &amp;gt; &amp;amp;maxval then flag=1;
   end;
   output;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Probably, the right answer is neither of these but uses some combination of these techniques.&amp;nbsp; I would guess you can work it out one way or the other.&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 20:35:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551247#M153150</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-15T20:35:25Z</dc:date>
    </item>
    <item>
      <title>Re: Use temporary array/do loop to check the value of one variable/one observation against many valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551251#M153151</link>
      <description>&lt;P&gt;The right answer depends on what you mean by this phrase:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I am trying to test whether the value of a variable for one observation is greater than the value of another variable for any observation in my table.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Let's call the two variables X and Y.&amp;nbsp; Do you want to find out if the value of X exceeds the MIN or the MAX value of Y?&amp;nbsp; Are you testing if X is larger than ANY (non-missing) value of Y? Or X is larger than ALL&amp;nbsp; values of Y?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here is code to make both flags.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select a.*
      , (a.X &amp;gt; max(a.Y)) as FLAG1 label="X larger than all Y"
      , (a.X &amp;gt; min(a.Y)) as FLAG2 label="X larger than any Y"
  from have a
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;If you did want to try your approach you need to realize you are pulling the VALUES of COLUMN1 into the macro variable and not the NAMES&amp;nbsp; of variables and adjust your ARRAY statement appropriately.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let value_list=.;
proc sql noprint;
select distinct Column1 into :value_list separated by ' '
from have;
%let a=&amp;amp;sqlobs;
quit;

....
array values (&amp;amp;a.) _temporary_ (&amp;amp;value_list);
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 20:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-temporary-array-do-loop-to-check-the-value-of-one-variable/m-p/551251#M153151</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-15T20:57:14Z</dc:date>
    </item>
  </channel>
</rss>

