<?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: Looping macro through list of subjects in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865837#M341930</link>
    <description>&lt;P&gt;Can you explain the larger problem that led you to thinking you needed to code this loopy stuff?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain the code you did write?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you running the query twice to get the count? PROC SQL already wrote the count into SQLOBS macro variable.&lt;/P&gt;
&lt;P&gt;The DISTINCT keyword is not a function call.&lt;/P&gt;
&lt;P&gt;Why are you creating the same dataset over and over in the LOOP?&amp;nbsp; If you don't do anything with it then only the last one will still exist when the looping is finished.&lt;/P&gt;
&lt;P&gt;Why are you using the STRIP() function?&amp;nbsp; Is subject numeric or character?&amp;nbsp; The STRIP() function is for character variables. But if you have a character variable then the way you wrote the IF statement cannot work as you are not comparing SUBJECT to a character string since you did not add quotes around the value.&amp;nbsp; Also if SUBJECT is character you do not want to use STRIP() because it will remove the LEADING spaces and then the resulting values will not match.&lt;/P&gt;
&lt;P&gt;Why are you telling the %SCAN() function to use both - and " as the delimiters when you built the macro variable using only - as the delimiter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming SUBJECT is character you probably want something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct quote(trim(subject))
   into :sb separated by '-'
from all1 
;
%let cnt=&amp;amp;sqlobs;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make a list like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"xyz"-"abc"-"def"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use a %DO loop like this to make a separate dataset for each subject.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;cnt ;
data subset&amp;amp;i;
  set all1;
  where subject  =  %scan(&amp;amp;sb,&amp;amp;i,-);
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you get something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset1;
  set all1;
  where subject  =  "xyz";
run;
data subset2;
  set all1;
  where subject  =  "abc";
run;
data subset3;
  set all1;
  where subject  =  "def";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you don't want to make separate datasets then remove the looping.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct quote(trim(subject))
   into :sb separated by ' '
from all1 
;
quit;
data want;
  set some_other_dataset;
  where subject in (&amp;amp;sb);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Mar 2023 02:42:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-03-23T02:42:49Z</dc:date>
    <item>
      <title>Looping macro through list of subjects</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865826#M341924</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to generate separate output for each subject. I programmed the report part now I have to filter the data for each subject which will then feed into report.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a first step I created a list of subjects separated by "'," and also the unique subject count.&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;select distinct(strip(subject)) into:sb separated by '-'&lt;BR /&gt;from all1 ;&lt;/P&gt;&lt;P&gt;select count(distinct subject) into:cnt&lt;BR /&gt;from all1 ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I am trying to loop this using do loop in a macro but the "if" condition is not filtering the data as desired.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i = 1 %to &amp;amp;cnt ;&lt;BR /&gt;data all2 ;&lt;BR /&gt;set all1;&lt;BR /&gt;if subject&amp;nbsp; =&amp;nbsp; %scan(&amp;amp;sb,&amp;amp;i,"-");&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 01:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865826#M341924</guid>
      <dc:creator>Leo9</dc:creator>
      <dc:date>2023-03-23T01:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro through list of subjects</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865834#M341929</link>
      <description>Maybe you need:&lt;BR /&gt;if subject  =  "%scan(&amp;amp;sb,&amp;amp;i,-)";&lt;BR /&gt;</description>
      <pubDate>Thu, 23 Mar 2023 01:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865834#M341929</guid>
      <dc:creator>Hao_Luo</dc:creator>
      <dc:date>2023-03-23T01:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro through list of subjects</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865837#M341930</link>
      <description>&lt;P&gt;Can you explain the larger problem that led you to thinking you needed to code this loopy stuff?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain the code you did write?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you running the query twice to get the count? PROC SQL already wrote the count into SQLOBS macro variable.&lt;/P&gt;
&lt;P&gt;The DISTINCT keyword is not a function call.&lt;/P&gt;
&lt;P&gt;Why are you creating the same dataset over and over in the LOOP?&amp;nbsp; If you don't do anything with it then only the last one will still exist when the looping is finished.&lt;/P&gt;
&lt;P&gt;Why are you using the STRIP() function?&amp;nbsp; Is subject numeric or character?&amp;nbsp; The STRIP() function is for character variables. But if you have a character variable then the way you wrote the IF statement cannot work as you are not comparing SUBJECT to a character string since you did not add quotes around the value.&amp;nbsp; Also if SUBJECT is character you do not want to use STRIP() because it will remove the LEADING spaces and then the resulting values will not match.&lt;/P&gt;
&lt;P&gt;Why are you telling the %SCAN() function to use both - and " as the delimiters when you built the macro variable using only - as the delimiter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming SUBJECT is character you probably want something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct quote(trim(subject))
   into :sb separated by '-'
from all1 
;
%let cnt=&amp;amp;sqlobs;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make a list like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"xyz"-"abc"-"def"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use a %DO loop like this to make a separate dataset for each subject.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;cnt ;
data subset&amp;amp;i;
  set all1;
  where subject  =  %scan(&amp;amp;sb,&amp;amp;i,-);
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you get something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset1;
  set all1;
  where subject  =  "xyz";
run;
data subset2;
  set all1;
  where subject  =  "abc";
run;
data subset3;
  set all1;
  where subject  =  "def";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you don't want to make separate datasets then remove the looping.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct quote(trim(subject))
   into :sb separated by ' '
from all1 
;
quit;
data want;
  set some_other_dataset;
  where subject in (&amp;amp;sb);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Mar 2023 02:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865837#M341930</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-23T02:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro through list of subjects</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865856#M341941</link>
      <description>&lt;P&gt;Keep in mind that all reporting procedures support BY, so you most probably don't need macro looping at all.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 06:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-through-list-of-subjects/m-p/865856#M341941</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-23T06:53:55Z</dc:date>
    </item>
  </channel>
</rss>

