<?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 over each item of a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855884#M338209</link>
    <description>Thanks so much, Tom. You helped me again.</description>
    <pubDate>Fri, 27 Jan 2023 01:38:31 GMT</pubDate>
    <dc:creator>windy</dc:creator>
    <dc:date>2023-01-27T01:38:31Z</dc:date>
    <item>
      <title>Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855674#M338143</link>
      <description>&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wrote a loop to run over each item stored in a macro variable, but the loop is partly working only.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I first write a macro called getdata and then write another macro called call_getdata to do the loop for each value of year and type stored in the macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If &amp;amp;year = "all", I want to get all observations. Similarly, if &amp;amp;type = "all", all observations are chosen. Otherwise, it only selects the data related to a particular year and type. I have a variable year and indicator in the datain sample containing the values of the years (2018, 2019, 2020, and 2021) and types (typea, typeb, typec)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the loop goes over specific values of year and type, it's working well. But when it goes over the first value "all", the loop doesn't work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The error message I received is "Variable all is not on file work.datain".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I specify option mprint, I saw that the way SAS reads the first %if %then %do %end is that it will select the observation whether year = "all" or type = "all".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help me to fix it?&lt;/P&gt;
&lt;PRE&gt;%macro getdata(datain,year,type);
/*get data*/
%if &amp;amp;year = "all" %then %do;
data data_out; set &amp;amp;datain; run;
%end;
%else %do;
data data_out; set &amp;amp;datain;
where year = &amp;amp;year;
run;
%end;

%if &amp;amp;type = "all" %then %do;
data datain_out_&amp;amp;type; set data_out; run;
%end;
%else %do;
data data_out_&amp;amp;type; set data_out;
where indicator = "&amp;amp;type";
run;
%end;
%mend;

%macro call_getdata();
%let years = all 2018 2019 2020 2021;
%let types =all typea typeb typec;
%local i j;
%do i=1 %to %sysfunc(countw(&amp;amp;years,%str( )));
%let year = %scan(&amp;amp;years,&amp;amp;i,%str( ));
%do j=1 %to %sysfunc(countw(&amp;amp;types,%str( )));
%let type = %scan(&amp;amp;types,&amp;amp;j,%str( ));

%let year = &amp;amp;year;
%let type = &amp;amp;type;
 
%getdata(datain=datain,year=&amp;amp;year,type=&amp;amp;type);

%end;
%end;

%mend;
%call_getdata();&lt;/PRE&gt;
&lt;P&gt;Thank you so much in advance for any help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 04:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855674#M338143</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-26T04:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855681#M338147</link>
      <description>&lt;P&gt;It really is not clear where you show an example passing a value of "all".&amp;nbsp; You need to show the code example where you pass a value of "all" for year, and another for type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since macro processor basically expects character values it almost never is a good idea to use quotes around values.&lt;/P&gt;
&lt;P&gt;When you use&lt;/P&gt;
&lt;P&gt;%if year="all" %then &amp;lt;whatever&amp;gt;&lt;/P&gt;
&lt;P&gt;YOU have to pass the value of "all" including the quotes. Example:&lt;/P&gt;
&lt;PRE&gt;%macro dummy(parm);
   %if &amp;amp;parm="all" %then %put "all" passed as parameter;
   %if &amp;amp;parm= all %then %put all pased as parameter;
%mend;

%dummy(all)
%dummy("all")&lt;/PRE&gt;
&lt;P&gt;Debug macros that are misbehaving by setting options Mprint:&lt;/P&gt;
&lt;PRE&gt;Options mprint;
&amp;lt;macro to test&amp;gt;
options nomprint; /* to turn off the option*/&lt;/PRE&gt;
&lt;P&gt;This will display the text generated by the macro in the LOG so you can see the values passed of the parameters. You could add SYMBOLGEN, to see details of how complex macro variables resolve, or MLOGIC to see how macro logical comparisons are built.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post code into a text or code box, opened on the forum with the &amp;lt;/&amp;gt; or "running man" icon above the main message window. That makes it easier to tell which is code and question. It will also maintain indenting. The main message window will reformat text and may reformat code so that it will not run by replacing white space with html that isn't seen.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 04:00:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855681#M338147</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-26T04:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855718#M338154</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let years = all 2018 2019 2020 2021;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For most SAS procedures, such a macro would not be needed and separate data sets are not needed. Most SAS procedures have a BY statement which repeats whatever analysis you want for each YEAR — and if necessary, you could simply repeat the code without the BY to have the analysis done on all data. If you just want means and other simple statistics, this last part of repeating the analysis code without the BY isn't even necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So before you struggle with all these macros, explain why you need this, and why using a BY statement isn't sufficient.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 11:43:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855718#M338154</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-26T11:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855723#M338157</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The actual loop is more complicated than this. I am doing a bunching estimation which the count of observations for each bin is essential. The count does not need to be in year or type. For each dataset, I will create variables using the counts and run a do-while loop. Finally, based on the optimized results from the loop, I will compute some estimators. That's why I think separating the sample from the beginning could make it easier.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855723#M338157</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-26T13:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855724#M338158</link>
      <description>&lt;P&gt;This part of your code is not going to work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;type = "all" %then %do;
data datain_out_&amp;amp;type; set data_out; run;
%end;
%else %do;
data data_out_&amp;amp;type; set data_out;
where indicator = "&amp;amp;type";
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If TYPE is equal to "all" then it will attempt to generate this SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain_out_"all"; set data_out; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which has an invalid dataset name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably meant to use one of these tests instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if "&amp;amp;type" = "all" %then %do;
%if &amp;amp;type = all %then %do;
%if %qupcase(&amp;amp;type) = ALL %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last one is the most flexible.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855724#M338158</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-26T13:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855725#M338159</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297250"&gt;@windy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The actual loop is more complicated than this. I am doing a bunching estimation which the count of observations for each bin is essential. The count does not need to be in year or type. For each dataset, I will create variables using the counts and run a do-while loop. Finally, based on the optimized results from the loop, I will compute some estimators. That's why I think separating the sample from the beginning could make it easier.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Okay,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297250"&gt;@windy&lt;/a&gt;&amp;nbsp;, thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am still skeptical that all of this macro code generation is needed (it might be, it's hard to tell). If you'd like to explore this further, the possibility of doing whatever you are doing without macros, let us know. We can talk about the details of what you are doing and whether or not the macro approach is the best way to go.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855725#M338159</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-26T13:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855727#M338160</link>
      <description>&lt;P&gt;A bit off-topic but:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Why not do the data read only one time (see macro getdata_short)?&amp;nbsp; [I fixed your `"all"` error in the code]&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain;
 do year = 2018 to 2023;
    do indicator = "typea","typeb","typec";
      x = ranuni(42);
      output;
    end;
 end;
run;
proc print;
run;


%macro getdata(datain,year,type);
/*get data*/
%if &amp;amp;year = all %then %do;
data data_out; set &amp;amp;datain; run;
%end;
%else %do;
data data_out; set &amp;amp;datain;
where year = &amp;amp;year;
run;
%end;

%if &amp;amp;type = all %then %do;
data data_out_&amp;amp;type; set data_out; run;
%end;
%else %do;
data data_out_&amp;amp;type; set data_out;
where indicator = "&amp;amp;type";
run;
%end;
%mend;



%macro getdata_short(datain,year,type);
data data2_out_&amp;amp;type; 
  set &amp;amp;datain;
  where 1
  %if &amp;amp;year NE all %then 
    %do;
      and year = &amp;amp;year 
    %end;
  %if &amp;amp;type NE all %then 
    %do;
      and indicator = "&amp;amp;type" 
    %end;
  ;
run;
%mend;

options mprint;
%getdata(datain,2020,typec);
%getdata_short(datain,2020,typec);

proc compare base=data_out_typec
              compare=data2_out_typec
              ;
run;

options mprint;
%getdata(datain,all,typec);
%getdata_short(datain,all,typec);

proc compare base=data_out_typec
              compare=data2_out_typec
              ;
run;

options mprint;
%getdata(datain,all,all);
%getdata_short(datain,all,all);

proc compare base=data_out_typec
              compare=data2_out_typec
              ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Other thing, if you execute:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%getdata(datain,2020,typec);

%getdata(datain,2021,typec);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the result of the second execution overwrites result of the first one, maybe adding the "year" value to the data set name?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) Since you are reading datain dataset several times to create several outputs, why not to read it only one time and use dedicated "output" statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro getdata_shorter(datain,years,types);
%local i ni j nj year type;

%let ni = %sysfunc(countw(%superq(years)));
%let nj = %sysfunc(countw(%superq(types)));

data
%do i = 1 %to &amp;amp;ni.;
  %do j = 1 %to &amp;amp;nj.;
    %let year = %scan(%superq(years),&amp;amp;i.);
    %let type = %scan(%superq(types),&amp;amp;j.);
    data_out_&amp;amp;year._&amp;amp;type.
  %end;
%end;
;
 
set &amp;amp;datain;

select;
  %do i = 1 %to &amp;amp;ni.;
    %do j = 1 %to &amp;amp;nj.;
      %let year = %scan(%superq(years),&amp;amp;i.);
      %let type = %scan(%superq(types),&amp;amp;j.);
        when (1=1
        %if &amp;amp;year NE all %then 
          %do;
            and year = &amp;amp;year 
          %end;
        %if &amp;amp;type NE all %then 
          %do;
            and indicator = "&amp;amp;type" 
          %end;
        ) output data_out_&amp;amp;year._&amp;amp;type.;
      %end;
  %end;

  otherwise;
end;

run;
%mend;


options mprint;
%getdata_shorter(datain,2020 2021 all,typec typeb all);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;MPRINT(GETDATA_SHORTER):   data data_out_2020_typec data_out_2020_typeb data_out_2020_all data_out_2021_typec data_out_2021_typeb
data_out_2021_all data_out_all_typec data_out_all_typeb data_out_all_all ;
MPRINT(GETDATA_SHORTER):   set datain;
MPRINT(GETDATA_SHORTER):   select;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2020 and indicator = "typec" ) output data_out_2020_typec;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2020 and indicator = "typeb" ) output data_out_2020_typeb;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2020 ) output data_out_2020_all;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2021 and indicator = "typec" ) output data_out_2021_typec;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2021 and indicator = "typeb" ) output data_out_2021_typeb;
MPRINT(GETDATA_SHORTER):   when (1=1 and year = 2021 ) output data_out_2021_all;
MPRINT(GETDATA_SHORTER):   when (1=1 and indicator = "typec" ) output data_out_all_typec;
MPRINT(GETDATA_SHORTER):   when (1=1 and indicator = "typeb" ) output data_out_all_typeb;
MPRINT(GETDATA_SHORTER):   when (1=1 ) output data_out_all_all;
MPRINT(GETDATA_SHORTER):   otherwise;
MPRINT(GETDATA_SHORTER):   end;
MPRINT(GETDATA_SHORTER):   run;

NOTE: There were 18 observations read from the data set WORK.DATAIN.
NOTE: The data set WORK.DATA_OUT_2020_TYPEC has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_2020_TYPEB has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_2020_ALL has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_2021_TYPEC has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_2021_TYPEB has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_2021_ALL has 1 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_ALL_TYPEC has 4 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_ALL_TYPEB has 4 observations and 3 variables.
NOTE: The data set WORK.DATA_OUT_ALL_ALL has 4 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855727#M338160</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-01-26T13:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855751#M338169</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've already got some great answers.&amp;nbsp; Just wanted to add some debugging thoughts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you're debugging macros, it often makes sense to debug the SAS code first, then debug the macro.&amp;nbsp; In the case where one macro calls another macro, you want to debug and test the called macro first, to get it working.&amp;nbsp; In this case you thought the problem was in your looping macro, but it's actually in the called macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you take your called macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro getdata(datain,year,type);
  %if &amp;amp;year = "all" %then %do;
    data data_out; set &amp;amp;datain; run;
  %end;
  %else %do;
    data data_out; set &amp;amp;datain;
      where year = &amp;amp;year;
    run;
  %end;

  %if &amp;amp;type = "all" %then %do;
    data datain_out_&amp;amp;type; set data_out; run;
  %end;
  %else %do;
    data data_out_&amp;amp;type; set data_out;
      where indicator = "&amp;amp;type";
    run;
  %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can test it with options MPRINT and MLOGIC turned on, to see what it does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%getdata(datain,2020,typec) works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%getdata(datain,all,typec) errors.&amp;nbsp; You can see in the log that the condition &amp;amp;year="all" evaluated to false (because of the quote marks):&lt;/P&gt;
&lt;PRE&gt;MLOGIC(GETDATA):  %IF condition &amp;amp;year = "all" is FALSE
MPRINT(GETDATA):   data data_out;
MPRINT(GETDATA):   set datain;
MPRINT(GETDATA):   where year = all;
ERROR: Variable all is not on file WORK.DATAIN.
MPRINT(GETDATA):   run;
&lt;/PRE&gt;
&lt;P&gt;%getdata(datain,"all",typec) works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%getdata(datain,"all","all") does not error, but it gives a bad result.&amp;nbsp; It outputs datain_out_ rather than datain_out_all, because of the quote marks:&lt;/P&gt;
&lt;PRE&gt;MLOGIC(GETDATA):  %IF condition &amp;amp;type = "all" is TRUE
MPRINT(GETDATA):   data datain_out_"all";
MPRINT(GETDATA):   set data_out;
MPRINT(GETDATA):   run;

NOTE: There were 18 observations read from the data set WORK.DATA_OUT.
NOTE: The data set WORK.DATAIN_OUT_ has 18 observations and 3 variables.
NOTE: The data set all has 18 observations and 3 variables.
&lt;/PRE&gt;
&lt;P&gt;I actually would have thought that last example, with&amp;nbsp;data datain_out_"all"; would error.&amp;nbsp; I guess the compiler just ignores&amp;nbsp; the quoted text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've already seen solutions, so I won't go further.&amp;nbsp; Just wanted to suggest that when you're running macros, you should have MPRINT turned on so that you can see the SAS code that is generated, and when you're debugging it's sometimes helpful to turn on MLOGIC and SYMBOLGEN.&amp;nbsp; More importantly, in complex macro settings, you want to debug the innermost macro first, and test it thoroughly.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855751#M338169</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-26T15:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855752#M338170</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If TYPE is equal to "all" then it will attempt to generate this SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datain_out_"all"; set data_out; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which has an invalid dataset name.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But, surprisingly to me, doesn't actually generate an error.&amp;nbsp; It just ignores the quoted code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    data datain_out_"all";
2    run;

NOTE: The data set WORK.DATAIN_OUT_ has 1 observations and 0 variables.
NOTE: The data set all has 1 observations and 0 variables
&lt;/PRE&gt;
&lt;P&gt;If you put a space inside the quoted text, it will error:&lt;/P&gt;
&lt;PRE&gt;3    data datain_out_"all all";
4    run;

ERROR: The value 'ALL ALL'n is not a valid SAS name.
NOTE: The SAS System stopped processing this step because of errors.
&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855752#M338170</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-26T15:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855753#M338171</link>
      <description>&lt;P&gt;it does not ignore it:&lt;/P&gt;
&lt;PRE&gt;NOTE: The data set all has 1 observations and 0 variables&lt;/PRE&gt;
&lt;P&gt;check out what do you have in&amp;nbsp; the "." directory&lt;/P&gt;
&lt;P&gt;I'll bet 5$ you will find there file named "all"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855753#M338171</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-01-26T15:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855755#M338172</link>
      <description>&lt;P&gt;Indeed, I managed to paste the log and didn't read it.&amp;nbsp; And here I am trying to give suggestions on debugging methods. : )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, it is there in the working directory.&amp;nbsp; I'll happily pay you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;the first time we get to meet in person.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855755#M338172</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-26T15:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855758#M338174</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;it does not ignore it:&lt;/P&gt;
&lt;PRE&gt;NOTE: The data set all has 1 observations and 0 variables&lt;/PRE&gt;
&lt;P&gt;check out what do you have in&amp;nbsp; the "." directory&lt;/P&gt;
&lt;P&gt;I'll bet 5$ you will find there file named "all"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So if is treating if as if there was a space between the two names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test "all";
  set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That syntax will create TEST in the WORK directory (or USER directory is using that option).&lt;/P&gt;
&lt;P&gt;But the ALL dataset will be created in the present working directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you don't have access to write in the PWD then you will get an error:&lt;/P&gt;
&lt;PRE&gt; 73         %let x="ALL";
 74         data xx&amp;amp;x ;
 75         run;
 
 ERROR: User does not have appropriate authorization level for library WC000001.
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set WORK.XX may be incomplete.  When this step was stopped there were 0 observations and 0 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855758#M338174</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-26T15:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855762#M338177</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;.&amp;nbsp; Makes sense.&amp;nbsp; I forgot that you can do stuff like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data "Q:\junk\me" ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and it will happily write Q:\junk\me.sas7bdat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855762#M338177</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-26T15:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855763#M338178</link>
      <description>&lt;P&gt;it's like:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data "C:\data\file1";
  set sashelp.class;
run;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855763#M338178</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-01-26T15:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855764#M338179</link>
      <description>&lt;P&gt;I wonder if it would work with "/dec/null" under linux &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855764#M338179</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-01-26T15:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855771#M338180</link>
      <description>&lt;P&gt;No.&amp;nbsp; "/dev/null" would mean a file named "/dev/null.sas7bdat" and Unix will not let your write a file into the /dev directory.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 15:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855771#M338180</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-26T15:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855884#M338209</link>
      <description>Thanks so much, Tom. You helped me again.</description>
      <pubDate>Fri, 27 Jan 2023 01:38:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855884#M338209</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-27T01:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over each item of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855885#M338210</link>
      <description>Thanks for your suggestion. I like the way you do. I tried the code. The log I got is similar to what you showed here, but the number of obs in datasets when &amp;amp;year = all are not correct yet. I'll recheck to see what's going on.</description>
      <pubDate>Fri, 27 Jan 2023 01:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-over-each-item-of-a-macro-variable/m-p/855885#M338210</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-27T01:42:17Z</dc:date>
    </item>
  </channel>
</rss>

