<?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 Appending Data Set in Macro Loop in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228374#M54274</link>
    <description>&lt;P&gt;This is my first post, so apologies if I'm missing something critical. Have spent time on Google and searching the SAS Community boards, but have not been able to find a solution (&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Proc-Append-then-Delete-in-a-loop/td-p/41602" target="_blank"&gt;this one&lt;/A&gt; and &lt;A href="http://stackoverflow.com/questions/17984153/is-it-possible-to-loop-over-sas-datasets" target="_blank"&gt;this one&lt;/A&gt; come close though).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what's going on:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I have a data set with 17 variables, some observations are missing datum&lt;/LI&gt;&lt;LI&gt;I would like to create a macro that will loop over the data set and create a new flag variable for each variable in the data set&lt;/LI&gt;&lt;LI&gt;The newly created flag variable(s) will be populated with a 0 or 1 depending on if the observation in the parent variable is missing datum&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;My interest in using a macro is because I could apply this to a variety of data sets, rather than typing everything out... This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;**********************************************************************;
*       Moneyball;
*       Last updated: 2015-10-04 by MJG;
**********************************************************************;

*       Shorten data name, save to work library;
data MB;
        set mydata.moneyball;
run; quit;
 
***********************************;
*       PROC CONTENTS;
***********************************;
*       List out the column names and data types for the data set;
proc contents data = MB out = MB_Contents;
run; quit;
 
*       Drop unnecessary variables gained from PROC CONTENTS;
data MB_Contents;
        set MB_Contents(keep = name type length varnum format formatl informat &lt;BR /&gt;informl just npos nobs);
run; quit;
 
*       View contents of data set, more info than PROC CONTENTS output;
proc print data = MB_Contents;
run; quit;
 
**********************************************************************;
*       Missing Flags;
**********************************************************************;
 
%let data_og = MB;
%let data_new = MB_MF;
%let contents = MB_Contents;
%let varname = name;
 
*       Macro for missing flags;
%macro MB_Missing(varname);
        data &amp;amp;data_new.;
                set &amp;amp;data_og.;
                        if missing(&amp;amp;varname.) then &amp;amp;varname._MF = 1;
                                else &amp;amp;varname._MF = 0;
        run; quit;
%mend;
 
*       Create missing flag variables and populate;
data &amp;amp;data_new.;
        do i = 1 to num;
                set &amp;amp;contents. nobs = num;
                        call execute('%MB_Missing('||name||')');
        end;
run; quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From what I can tell (via the SAS log), the loop actually works and creates the flag variables as intended. The only hang up is that when I do a PROC PRINT of the resulting data set, MB_MF, only the &lt;EM&gt;last&lt;/EM&gt; flag variable created is in there, the rest are not. So my suspicion is that I'm missing an APPEND or even PUT (?) somewhere in there?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am fairly new to SAS so if there's something in my code that feels like nails on a chalkboard, please let me know!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;</description>
    <pubDate>Mon, 05 Oct 2015 01:59:44 GMT</pubDate>
    <dc:creator>mgilbert</dc:creator>
    <dc:date>2015-10-05T01:59:44Z</dc:date>
    <item>
      <title>Appending Data Set in Macro Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228374#M54274</link>
      <description>&lt;P&gt;This is my first post, so apologies if I'm missing something critical. Have spent time on Google and searching the SAS Community boards, but have not been able to find a solution (&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Proc-Append-then-Delete-in-a-loop/td-p/41602" target="_blank"&gt;this one&lt;/A&gt; and &lt;A href="http://stackoverflow.com/questions/17984153/is-it-possible-to-loop-over-sas-datasets" target="_blank"&gt;this one&lt;/A&gt; come close though).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what's going on:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I have a data set with 17 variables, some observations are missing datum&lt;/LI&gt;&lt;LI&gt;I would like to create a macro that will loop over the data set and create a new flag variable for each variable in the data set&lt;/LI&gt;&lt;LI&gt;The newly created flag variable(s) will be populated with a 0 or 1 depending on if the observation in the parent variable is missing datum&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;My interest in using a macro is because I could apply this to a variety of data sets, rather than typing everything out... This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;**********************************************************************;
*       Moneyball;
*       Last updated: 2015-10-04 by MJG;
**********************************************************************;

*       Shorten data name, save to work library;
data MB;
        set mydata.moneyball;
run; quit;
 
***********************************;
*       PROC CONTENTS;
***********************************;
*       List out the column names and data types for the data set;
proc contents data = MB out = MB_Contents;
run; quit;
 
*       Drop unnecessary variables gained from PROC CONTENTS;
data MB_Contents;
        set MB_Contents(keep = name type length varnum format formatl informat &lt;BR /&gt;informl just npos nobs);
run; quit;
 
*       View contents of data set, more info than PROC CONTENTS output;
proc print data = MB_Contents;
run; quit;
 
**********************************************************************;
*       Missing Flags;
**********************************************************************;
 
%let data_og = MB;
%let data_new = MB_MF;
%let contents = MB_Contents;
%let varname = name;
 
*       Macro for missing flags;
%macro MB_Missing(varname);
        data &amp;amp;data_new.;
                set &amp;amp;data_og.;
                        if missing(&amp;amp;varname.) then &amp;amp;varname._MF = 1;
                                else &amp;amp;varname._MF = 0;
        run; quit;
%mend;
 
*       Create missing flag variables and populate;
data &amp;amp;data_new.;
        do i = 1 to num;
                set &amp;amp;contents. nobs = num;
                        call execute('%MB_Missing('||name||')');
        end;
run; quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From what I can tell (via the SAS log), the loop actually works and creates the flag variables as intended. The only hang up is that when I do a PROC PRINT of the resulting data set, MB_MF, only the &lt;EM&gt;last&lt;/EM&gt; flag variable created is in there, the rest are not. So my suspicion is that I'm missing an APPEND or even PUT (?) somewhere in there?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am fairly new to SAS so if there's something in my code that feels like nails on a chalkboard, please let me know!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 01:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228374#M54274</guid>
      <dc:creator>mgilbert</dc:creator>
      <dc:date>2015-10-05T01:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Appending Data Set in Macro Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228378#M54275</link>
      <description>&lt;P&gt;You want each iteration to add a new variable, so you should be overwriting the dataset, not starting from the original on each iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*       Macro for missing flags;
%macro MB_Missing(varname);
data &amp;amp;data_og.;
set &amp;amp;data_og.;
&amp;amp;varname._MF = missing(&amp;amp;varname.);
run;
%mend;
 
*       Create missing flag variables and populate;
data _null_;
set &amp;amp;contents. ;
call execute('%MB_Missing('||name||');');
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: &lt;STRONG&gt;quit;&lt;/STRONG&gt; is ignored after &lt;STRONG&gt;run;&lt;/STRONG&gt; except for interactive procedures such as &lt;STRONG&gt;proc reg &lt;/STRONG&gt;that accept multiple &lt;STRONG&gt;run;&lt;/STRONG&gt; statements.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 03:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228378#M54275</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-10-05T03:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Appending Data Set in Macro Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228403#M54278</link>
      <description>&lt;P&gt;Thank you! Worked perfect, and learned a new way to do the IF-THEN-ELSE logic. Greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I wanted to preserve MB (data_og), and simply create a new data set with these additional variables is the best way to do that a data step outside of the macro and populate step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 11:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228403#M54278</guid>
      <dc:creator>mgilbert</dc:creator>
      <dc:date>2015-10-05T11:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Appending Data Set in Macro Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228445#M54282</link>
      <description>Copy MB to MB_MF and do the update from there.</description>
      <pubDate>Mon, 05 Oct 2015 14:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Appending-Data-Set-in-Macro-Loop/m-p/228445#M54282</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-10-05T14:55:19Z</dc:date>
    </item>
  </channel>
</rss>

