<?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: SAS code review for macro variables and modification to export data sets to local drive in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851148#M336382</link>
    <description>&lt;P&gt;In your first LIBNAME, you use a typical UNIX path, but in your second LIBNAME, you have a backslash, pointing to a Windows system. If one of those works, the other can't work, depending on the operating system where SAS executes.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What kind of SAS installation do you use? Single machine (desktop), or client/server?&lt;/LI&gt;
&lt;LI&gt;If the latter, which operating system on the server?&lt;/LI&gt;
&lt;LI&gt;Which interface do you use? Enterprise Guide, SAS Studio, or Display Manager (if single machine)?&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Mon, 26 Dec 2022 16:51:32 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-12-26T16:51:32Z</dc:date>
    <item>
      <title>SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851141#M336378</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;I want to create a programme that will run through all the datasets in a library and then use the proc freq to see the missing values in each datasets. Then export it to my local drive.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a code and would like to modify it a bit. I want to change the red highlighted text with the green highlighted ones. so that I can run the proc freq. Now the problem is I do not know how the create a macro for each datasets (like&amp;nbsp;&lt;CODE class=""&gt;&lt;FONT color="#99CC00"&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;gphe.MHPRESP1001,&amp;nbsp;it&amp;nbsp;should&amp;nbsp;run&amp;nbsp;for&amp;nbsp;all&amp;nbsp;the&amp;nbsp;73&amp;nbsp;datasets&amp;nbsp;in&amp;nbsp;the&amp;nbsp;libra&lt;FONT color="#000000"&gt;r&lt;/FONT&gt;&lt;/STRONG&gt;y&lt;/FONT&gt;)&lt;/FONT&gt;&lt;/CODE&gt;&lt;/P&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;PRE&gt;&lt;CODE class=""&gt;%let var1=gphl;
%let var2=gphe;


%macro xyz;

%do i=1 %to 2;

libname &amp;amp;&amp;amp;var&amp;amp;i. "/abc_&amp;amp;&amp;amp;var&amp;amp;i./prelock/data/raw/shared" access=readonly;

&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;data &amp;amp;&amp;amp;var&amp;amp;i.._test;
set &amp;amp;&amp;amp;var&amp;amp;i...MHPRESP1001;
where MHDXDAT   MHOCCUR   MHSEV   MHSPID   MHTERM   MHSTDAT   MHONGO   MHENDAT   MHCAT_4   MHPRESP   MHLLTCDPRESP   DICT   DICTVER eq '';
run;&lt;/STRONG&gt;&lt;/FONT&gt;
%end;

%mend;
%xyz;
&lt;FONT color="#99CC00"&gt;ods output nlevels= MHPRESP1001;
proc freq data= gphe.MHPRESP1001 nlevels;
tables _all_ ;
run;
ods select all;&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then my plan is to export all the datasets from WORK lib to my local folder by using below&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;libname outfile xlsx "&amp;amp;path.\Forecast.xlsx";

proc copy in=work out=outfile;
run;

libname outfile;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please can anyone help ?&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 15:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851141#M336378</guid>
      <dc:creator>Snum</dc:creator>
      <dc:date>2022-12-26T15:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851147#M336381</link>
      <description>&lt;P&gt;The first step in the process of creating a macro is to create &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WORKING&lt;/STRONG&gt;&lt;/FONT&gt; code for two cases of your situation, hard-coded (in this case, two data set names) that does what you want without macros and without macro variables. Emphasis on &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WORKING&lt;/STRONG&gt;&lt;/FONT&gt;. Right now your code doesn't work, and will not run.&lt;BR /&gt;&lt;BR /&gt;So please provide us with &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;WORKING&lt;/STRONG&gt;&lt;/FONT&gt; code for your situation, without macros and without macro variables, for two data sets hard coded. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Specifically, this line of code doesn't work, and there may be other errors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where MHDXDAT   MHOCCUR   MHSEV   MHSPID   MHTERM   MHSTDAT   MHONGO   MHENDAT   MHCAT_4   MHPRESP   MHLLTCDPRESP   DICT   DICTVER eq '';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Dec 2022 16:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851147#M336381</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-26T16:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851148#M336382</link>
      <description>&lt;P&gt;In your first LIBNAME, you use a typical UNIX path, but in your second LIBNAME, you have a backslash, pointing to a Windows system. If one of those works, the other can't work, depending on the operating system where SAS executes.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What kind of SAS installation do you use? Single machine (desktop), or client/server?&lt;/LI&gt;
&lt;LI&gt;If the latter, which operating system on the server?&lt;/LI&gt;
&lt;LI&gt;Which interface do you use? Enterprise Guide, SAS Studio, or Display Manager (if single machine)?&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 26 Dec 2022 16:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851148#M336382</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-12-26T16:51:32Z</dc:date>
    </item>
    <item>
      <title>Solved SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851239#M336430</link>
      <description>&lt;P&gt;I got it after much struggle and doing research (as I am not a programmer and a new bird in SAS).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql; ** to get the count of datasets in a Lib**;
create table  My as
select distinct memname, libname from dictionary.tables where Libname="&amp;amp;lib" and memtype='DATA';

select distinct count(memname) into :cnt from my;
select distinct memname into :dataset1 - :dataset%left(&amp;amp;cnt) from my;
quit; 
**** macro to run thorough all the datasets in a Lib ***;
%macro Abc;
%do i= 1 %to &amp;amp;cnt;
ods output nlevels= &amp;amp;&amp;amp;dataset&amp;amp;i.;
proc freq data= &amp;amp;lib..&amp;amp;&amp;amp;dataset&amp;amp;i. nlevels;
tables _all_ ;
run;
ods select all;
data out.&amp;amp;&amp;amp;dataset&amp;amp;i.; 
set &amp;amp;&amp;amp;dataset&amp;amp;i.;
where NNonMissLevels=0;
run;
%end;
%mend;
%Abc;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Dec 2022 13:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851239#M336430</guid>
      <dc:creator>Snum</dc:creator>
      <dc:date>2022-12-27T13:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Solved SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851257#M336438</link>
      <description>&lt;P&gt;You are working way too hard to get the list of datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct memname into :dataset1 -
  from dictionary.tables
  where libname=%upcase("&amp;amp;lib") and memtype='DATA'
;
%let cnt=&amp;amp;sqlobs;
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That type of code generation is much easier without macro code.&amp;nbsp; Just use a data step to generate the code directly from the source data without first transferring the data into macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also might be easier to just build on single dataset that has the NLEVELS data for ALL of the datasets.&lt;/P&gt;
&lt;P&gt;So first get the list of members and use that to generate the code to summarize each one and append the results to the summary dataset.&amp;nbsp; Since you will want to include information about the dataset that the variable live in you could use MODIFY statement in a data step to append instead of using PROC APPEND. Then it is easy to add the dataset name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So here is code to get the dataset list and use it to generate the code to call PROC FREQ and append the results.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=&amp;amp;lib.._all_ noprint out=contents;
run;
filename code temp;
data _null_;
  set contents;
  by memname;
  if first.memname;
  file code;
  put 'ods output nlevels=nlevels;';
  put 'proc freq data=' libname +(-1) '.' memname 'nlevels;tables _all_/noprint;run;' ;
  put 'data summary;'
    / 'if _n_=0 then modify summary;'
    / 'set nlevels;'
    / libname = :$quote. ';'
    / memname = :$quote. ';'
    / 'output;'
    / 'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now setup an empty SUMMARY dataset and then run the generated code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data summary;
  length libname $8 memname $32 TableVar $32 NLevels NMissLevels NNonMissLevels 8
         TableVarLabel $256
  ;
  stop;
  call missing(of _all_);
run;

ods select none;
%include code / source2;
ods select all;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can pick from that SUMMARY dataset the observations you want.&amp;nbsp; Do you want those with no non-missing values?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set summary ;
  where NNonMissLevels &amp;lt; 1 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2022 16:44:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851257#M336438</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-27T16:44:12Z</dc:date>
    </item>
    <item>
      <title>Re: Solved SAS code review for macro variables and modification to export data sets to local drive</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851295#M336456</link>
      <description>Thanks a lot TOM for looking into my code :). Your code worked . and Thanks a lot for the detailed explanation and wish you a happy new year .</description>
      <pubDate>Wed, 28 Dec 2022 03:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-review-for-macro-variables-and-modification-to-export/m-p/851295#M336456</guid>
      <dc:creator>Snum</dc:creator>
      <dc:date>2022-12-28T03:21:18Z</dc:date>
    </item>
  </channel>
</rss>

