<?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: Do Loops are doing my head in. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50039#M13596</link>
    <description>Thank you so much to you both.  You did a fantastic Job.</description>
    <pubDate>Sun, 28 Sep 2008 22:31:31 GMT</pubDate>
    <dc:creator>Scottcom4</dc:creator>
    <dc:date>2008-09-28T22:31:31Z</dc:date>
    <item>
      <title>Do Loops are doing my head in.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50036#M13593</link>
      <description>Hi Guys,&lt;BR /&gt;
&lt;BR /&gt;
I am attempting to find and output the last record within a dataset.  I know.....I know it is sooooo simple, however I am coming unstuck because I am attempting to do so where a condition is met and on some occasions I may need to output more than one record.  For instance.&lt;BR /&gt;
&lt;BR /&gt;
Data:&lt;BR /&gt;
&lt;BR /&gt;
Code	Product	0	1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20	21	22	23-55	56+&lt;BR /&gt;
069   	Motor	25	182	67	217	88	39	36	5	1	40	12	8	8	7	4	0	14	7	4	1	6	1	0	61	45&lt;BR /&gt;
070   	Motor	18	114	28	11	44	21	19	16	12	3	0	9	5	11	7	6	2	1	6	6	3	2	5	52	60&lt;BR /&gt;
071   	Motor	153	102	12	6	23	12	5	4	5	0	0	1	3	1	1	1	0	1	4	1	0	1	2	0	0&lt;BR /&gt;
072   	Motor	259	786	559	74	21	91	55	63	39	21	4	1	15	16	17	12	11	1	0	5	8	8	0              0              0&lt;BR /&gt;
&lt;BR /&gt;
In this case I want to output both code 071 and 072 to  2 dataset (Outstanding071 and Outstanding072), because both of these codes have outstanding tasks.  I gather a Do Loop would do the job (although I could be wrong), but have no idea what to do and keep crashing the server (whoops).  &lt;BR /&gt;
&lt;BR /&gt;
Does anyone have any ideas how I can rectify this (please be kind).&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Scott</description>
      <pubDate>Fri, 26 Sep 2008 06:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50036#M13593</guid>
      <dc:creator>Scottcom4</dc:creator>
      <dc:date>2008-09-26T06:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops are doing my head in.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50037#M13594</link>
      <description>Hi:&lt;BR /&gt;
  I'm not sure whether you really need a DO loop or not. Basically, the DATA step program is an implied loop -- it will repeat the statements in the program for every dataline or row in the input data.&lt;BR /&gt;
&lt;BR /&gt;
  Consider this program. It reads in data that resembles your example (without so many values), calculates the sum of the numbers on each row (excluding CODE and PRODUCT) and creates 2 output data sets, based on the value of CODE. Since I didn't know what your criteria were for deciding that something was outstanding, I just did the output based on the value of CODE:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data testcode;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input code product $ var0 var1 var2 var3 var4;&lt;BR /&gt;
  format code z3.;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
069 Motor 25 182 67 217 88 &lt;BR /&gt;
070 Motor 18 114 28 11 44 &lt;BR /&gt;
071 Motor 153 102 12 6 23 &lt;BR /&gt;
072 Motor 259 786 559 74 21&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
data out071 out072;&lt;BR /&gt;
  set testcode;&lt;BR /&gt;
  tot = sum(of var0-var4);&lt;BR /&gt;
  if code = 71 then output out071;&lt;BR /&gt;
  else if code = 72 then output out072;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
proc print data=out071;&lt;BR /&gt;
  title '071';&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
proc print data=out072;&lt;BR /&gt;
  title '072';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
If you need to create a more dynamic program, then you may need to investigate using a SAS macro program to dynamically find out which records meet your criteria and create the appropriately named output datasets.&lt;BR /&gt;
&lt;BR /&gt;
If you decide that you do need to use SAS Macro processing for your task, then the key thing is to start with a working SAS program. Make sure you have the program logic working in a 'regular' program before you try to introduce macro elements into the code.&lt;BR /&gt;
&lt;BR /&gt;
For more information about the SAS Macro Facility, the documentation is quite good and this paper provides a good introduction:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Fri, 26 Sep 2008 07:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50037#M13594</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-09-26T07:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops are doing my head in.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50038#M13595</link>
      <description>Hi Scottcom4&lt;BR /&gt;
Cynthia gave you already a lot of input. I was just wondering about "...to find and output the last record..."; but then you're talking about the "codes" which have nothing to do with "the last record" - and these codes are also unique in your example; so it's also not the last record of a by group (by code)???&lt;BR /&gt;
Looking at your example I also didn't understand how you identify outstanding records.&lt;BR /&gt;
&lt;BR /&gt;
In general:&lt;BR /&gt;
You use loops to perform a repetitive task within an Observarion ('record') - a "line of data" to express it a bit un-IT.&lt;BR /&gt;
&lt;BR /&gt;
If you're rather unexperience with programming then I would be careful to go into too much SAS macro programming before you understand the basics. SAS macro programming IS a very powerfull addition - but I've seen too many examples where people got lost in macro code only because they didn't understand the basics (and SAS macro coding wouldn't have been necessary at all).&lt;BR /&gt;
I can't agree with Cynthia that the SAS Macro manual is that good (at least in comparison to the outstanding BASE SAS manual).&lt;BR /&gt;
&lt;BR /&gt;
Have a look at the example code below! I hope this will be helpful for you.&lt;BR /&gt;
I can only recommend you to look up and understand how "into" works together with Proc SQL - it's not too difficult to learn and very powerful.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  infile datalines truncover;&lt;BR /&gt;
  input code:$3. product:$20. var1-var25;&lt;BR /&gt;
datalines;&lt;BR /&gt;
069 Motor 25 182 67 217 88 39 36 5 1 40 12 8 8 7 4 0 14 7 4 1 6 1 0 61 45&lt;BR /&gt;
070 Motor 18 114 28 11 44 21 19 16 12 3 0 9 5 11 7 6 2 1 6 6 3 2 5 52 60&lt;BR /&gt;
071 Motor 153 102 12 6 23 12 5 4 5 0 0 1 3 1 1 1 0 1 4 1 0 1 2 0 0&lt;BR /&gt;
072 Motor 259 786 559 74 21 91 55 63 39 21 4 1 15 16 17 12 11 1 0 5 8 8 0 0 0&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
/* output all records where two consecutive VARn values are 0 */&lt;BR /&gt;
data OutstandingAll;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  drop i;&lt;BR /&gt;
  array vars {25} var1-var25;&lt;BR /&gt;
  do i=1 to dim(vars)-1;&lt;BR /&gt;
    if vars{i}=0 and vars{i+1}=0 then&lt;BR /&gt;
    do;&lt;BR /&gt;
      output;&lt;BR /&gt;
      leave;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/*----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;
  Now comes the more difficult part: We want to output the records to datasets where part of the &lt;BR /&gt;
  dataset-name is the value of the variable 'code' with outstanding records. The following is a dynamic solution where&lt;BR /&gt;
  you don't have to define all the possible dataset names in advance (codes might change...)&lt;BR /&gt;
  The approach is: &lt;BR /&gt;
  - Use Proc SQL with Distinct to find all Unique codes in the dataset "OutstandingAll" (=all records which are outstanding).&lt;BR /&gt;
  - Write the result into macro variables (which we can use later on in another datastep). What the code &lt;BR /&gt;
    does is concatenating text with variable values and storing them into two macro variables. We can use&lt;BR /&gt;
    these macro vars later on. They are resolved by the macro processor (which runs always before the 'Base'&lt;BR /&gt;
    SAS processor. So the SAS processor sees the resolved macro vars (have a look in the log - there you see&lt;BR /&gt;
    to what they resolve) and uses the text in the macrovars.&lt;BR /&gt;
  - The macro var &amp;amp;DatasetList contains all the needed names for the output tables.&lt;BR /&gt;
  - The macro var &amp;amp;WhenClause contains the SAS Statements to write the SAS observations to the corresponding SAS dataset&lt;BR /&gt;
&lt;BR /&gt;
  Look up "into" as part of Proc SQL. This is VERY powerful and helps avoid a lot of coding.&lt;BR /&gt;
--------------------------------------------------------------------------------------------------------------------------*/&lt;BR /&gt;
 &lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select distinct cats('work.','Outstanding',code),cats('when ("',code,'") output Outstanding',code,';')  &lt;BR /&gt;
            into :DatasetList separated by ' ', :WhenClause separated by ' '&lt;BR /&gt;
    from OutstandingAll;&lt;BR /&gt;
  %put &amp;amp;DatasetList;&lt;BR /&gt;
  %put %bquote(&amp;amp;WhenClause);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
data _null_ &amp;amp;DatasetList;&lt;BR /&gt;
  set OutstandingAll;&lt;BR /&gt;
  select(code);&lt;BR /&gt;
    &amp;amp;WhenClause&lt;BR /&gt;
    otherwise;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;</description>
      <pubDate>Fri, 26 Sep 2008 14:55:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50038#M13595</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2008-09-26T14:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops are doing my head in.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50039#M13596</link>
      <description>Thank you so much to you both.  You did a fantastic Job.</description>
      <pubDate>Sun, 28 Sep 2008 22:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50039#M13596</guid>
      <dc:creator>Scottcom4</dc:creator>
      <dc:date>2008-09-28T22:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops are doing my head in.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50040#M13597</link>
      <description>Thank you

Message was edited by: Scottcom4</description>
      <pubDate>Mon, 29 Sep 2008 02:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Do-Loops-are-doing-my-head-in/m-p/50040#M13597</guid>
      <dc:creator>Scottcom4</dc:creator>
      <dc:date>2008-09-29T02:01:04Z</dc:date>
    </item>
  </channel>
</rss>

