<?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 to implement nested loop structures in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91542#M26107</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think it can be done this way, using a single MACRO variable (untested) :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; select distinct dataType into :inCmd separated by " " from datatypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; data allDatatypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; set &amp;amp;inCmd indsname=dataType;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; select S.subjId as pt, D.dataType as dt, /* and whatever you need */&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; from subjects as S inner join allDataTypes&amp;nbsp; as D on&amp;nbsp; S.subjId=D.subjId;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; drop allDataTypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 05 Dec 2012 22:54:44 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2012-12-05T22:54:44Z</dc:date>
    <item>
      <title>How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91541#M26106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to implement a 2 level nested loop in SAS. Please see SAS pseudo code below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a list of subject numbers held in the dataset "subjects" and a list of dataset names held in dataset "datatypes". Each of the datatypes referenced by the dataset "datatypes" would be an individual dataset. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set subjects;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pt = subjid;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set datatypes;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dt = datatype;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * print all the data for the current pt and dt, eg, "select * from dt where subjid = pt"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Run;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please note that its the code for the 2 level nested loop that I need and not the how to implement the specific code within the loops.&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 22:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91541#M26106</guid>
      <dc:creator>Mikeyjh</dc:creator>
      <dc:date>2012-12-05T22:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91542#M26107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think it can be done this way, using a single MACRO variable (untested) :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; select distinct dataType into :inCmd separated by " " from datatypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; data allDatatypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; set &amp;amp;inCmd indsname=dataType;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; select S.subjId as pt, D.dataType as dt, /* and whatever you need */&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; from subjects as S inner join allDataTypes&amp;nbsp; as D on&amp;nbsp; S.subjId=D.subjId;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; drop allDataTypes;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt; quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 22:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91542#M26107</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-12-05T22:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91543#M26108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your reply PG.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can you explain what your allDataTypes statement does especially the "indsname=dataType"?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 23:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91543#M26108</guid>
      <dc:creator>Mikeyjh</dc:creator>
      <dc:date>2012-12-05T23:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91544#M26109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Why do you feel the need to nest it? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sounds like you want to make a patient profile report.&lt;/P&gt;&lt;P&gt;One way is to create a macro that reports one patient then generate a call to the macro for each patient.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro patprofile(ptid);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%local dslist ds i ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%let dslist = dm ae ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;dslist));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %let ds=%scan(&amp;amp;dslist,&amp;amp;it);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; proc print data=&amp;amp;ds ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where ptid=&amp;amp;ptid; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%mend patprofile;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;filename code temp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set patlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; put '%patprofile(' ptid ');';&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%inc code / source2 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 23:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91544#M26109</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-12-05T23:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91545#M26110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's not clear how general this needs to be, but here's another approach.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set datatypes;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; call execute('data ' || strip(datatype) || '; set subjects; run;');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the requirements become really involved, this might get cumbersome.&amp;nbsp; But it might do the job that you require.&amp;nbsp; Here's another thought as well that begins the same as PGStats' suggestion:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; select distinct strip(datatype) into : dataset_list separated by ' ' from datatypes&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data &amp;amp;dataset_list;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set subjects;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, this doesn't work if you need a more involved analysis, such as a PROC FREQ for each data set.&amp;nbsp; But if that might be needed, there are definitely ways to loop through each data set name in a list.&amp;nbsp; See how close you come to what you need with this much, and we can always embellish later.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 23:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91545#M26110</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-12-05T23:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement nested loop structures</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91546#M26111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The first step creates a list of the dataset names.&lt;/P&gt;&lt;P&gt;The second step reads and concatenates all those datasets in the list and creates a new variable called dataType to keep the name of the dataset that contributed each observation.&lt;/P&gt;&lt;P&gt;The third step is a query that joins the Subjects dataset with the allDataTypes dataset by pairing observations with the same subjId.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hth.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Dec 2012 00:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-implement-nested-loop-structures/m-p/91546#M26111</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-12-06T00:48:27Z</dc:date>
    </item>
  </channel>
</rss>

