<?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: Macro to create a tempory file [Windows] in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252692#M48009</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;has the simplest method,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;filename your_file "%sysfunc(pathname(work))\your_file.txt";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would get removed automatically at end of session then.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I am sure as part of the ongoing lifecycle management of those macros you would have a plan in place to review the code, identify new technologies/procedures etc. in subsequent releases of SAS and so would need to update these ongoing anyways, if thats the case then perhpaps new technology would remove the need for the temporary files? &amp;nbsp;For instance, the graphs produced, could be created as template files which are stored, then you just put whatever data you want through those, no need to create text files with the code.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Feb 2016 09:54:34 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-02-26T09:54:34Z</dc:date>
    <item>
      <title>Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252554#M47971</link>
      <description>&lt;P&gt;As some of you may know, I have a large collection of (legacy) sas macros (&lt;A href="http://www.datavis.ca/sasmac/" target="_blank"&gt;http://www.datavis.ca/sasmac/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Some of these need to&lt;/P&gt;
&lt;P&gt;create a temporary file and I've used the utility macros below in a quite a few top-level macros.&amp;nbsp; Under Windows, there has always been the problem that it tries to use `drive:\temp` as the directory, and this fails if that directory doesn't exist.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suppose I could just use `.` (the current directory), but perhaps someone has a better way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* ---------------------------------------------------------------
TITLE:  Handle temporary files in a system- and version-independent way.
 --------------------------------------------------------------- */

%macro tempfile(fileref,ls,options);
   %global tempfn;
   %if %length(&amp;amp;ls)=0 %then %let ls=80;

   %if %sysevalf(&amp;amp;sysver  &amp;gt; 6.10) %then %do;
		filename &amp;amp;fileref temp &amp;amp;options
			%if %length(&amp;amp;ls)&amp;gt;0 %then lrecl=&amp;amp;ls;
			;		
		%end;
	%else %do;
		%if &amp;amp;sysscp = CMS
			%then %let tempfn=&amp;amp;fileref output a;
		%else %if &amp;amp;sysscp = WIN 
			%then %let tempfn=&amp;amp;fileref..tmp;
		%else /* assume unix */ 
			%let tempfn=/tmp/&amp;amp;fileref..tmp;
		filename &amp;amp;fileref "&amp;amp;tempfn" lrecl=&amp;amp;ls &amp;amp;options;
		%end;
%mend;

%macro tempdel(fileref);
   %global tempfn;
	%if length(&amp;amp;tempfn)=0 %then %goto done;
    *-- Avoid annoying flash with X commands;
    %if %sysevalf(&amp;amp;sysver  &amp;gt; 6.10) %then %do;
        %let rc=%sysfunc(fdelete(&amp;amp;fileref));
        %let rc=%sysfunc(filename(&amp;amp;fileref,''));
    %end;

    %else %do;
		%if &amp;amp;sysscp = CMS
			%then cms erase &amp;amp;tempfn;
		%else %if &amp;amp;sysscp = WIN
			%then %do;
				options noxsync noxwait; run;
				%sysexec(erase &amp;amp;tempfn); run;
				options   xsync   xwait; run;
			%end;
		%else /* assume flavor of UNIX */
				%sysexec(rm -f &amp;amp;tempfn);
    %end;
	 filename &amp;amp;fileref clear;
	 %let tempfn=;
%done:
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252554#M47971</guid>
      <dc:creator>michael_friendly</dc:creator>
      <dc:date>2016-02-25T22:17:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252559#M47973</link>
      <description>&lt;P&gt;Couldn't you just create it on the fly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x 'mkdir temp';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That could go right before the FILENAME statement.&amp;nbsp; It might hiccup if that directory already exists, but you can test it to see if you get the result you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course if you ever want to find the directory, you have to know where it will be located.&amp;nbsp; But that's another story for another day.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252559#M47973</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-02-25T22:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252561#M47974</link>
      <description>&lt;P&gt;The link of your legacy macros is not working ! Never mind, was able to find it. &amp;nbsp;Thanks !&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252561#M47974</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2016-02-25T22:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252564#M47976</link>
      <description>&lt;P&gt;Fixed.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:18:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252564#M47976</guid>
      <dc:creator>michael_friendly</dc:creator>
      <dc:date>2016-02-25T22:18:25Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252566#M47977</link>
      <description>&lt;P&gt;Under Windows I avoid 'x' commands, and try to avoid hicupps too.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252566#M47977</guid>
      <dc:creator>michael_friendly</dc:creator>
      <dc:date>2016-02-25T22:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252568#M47978</link>
      <description>&lt;P&gt;Well, you could always use %sysexec instead of X.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are functions to check everything ... does a file exist, does a SAS data set exist ... there must be one that checks whether a folder exists.&amp;nbsp; Since you're in the middle of a macro, assuming you find the proper function, you could use %IF/%THEN to determine whether or not to issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%sysexec mkdir temp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Quotes are no longer needed when you switch from X to %SYSEXEC.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252568#M47978</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-02-25T22:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252569#M47979</link>
      <description>&lt;P&gt;What about finding the path to the work library and using that? It seems like a good idea to me, since it will also get cleaned up in case?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 22:32:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252569#M47979</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-25T22:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252605#M47987</link>
      <description>&lt;P&gt;Michael,&lt;/P&gt;
&lt;P&gt;I don't know if the following can fit your macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;filename x temp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you use this TEMP filename at anywhere at any version of OS, if I was right .&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2016 01:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252605#M47987</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-26T01:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252692#M48009</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;has the simplest method,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;filename your_file "%sysfunc(pathname(work))\your_file.txt";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would get removed automatically at end of session then.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I am sure as part of the ongoing lifecycle management of those macros you would have a plan in place to review the code, identify new technologies/procedures etc. in subsequent releases of SAS and so would need to update these ongoing anyways, if thats the case then perhpaps new technology would remove the need for the temporary files? &amp;nbsp;For instance, the graphs produced, could be created as template files which are stored, then you just put whatever data you want through those, no need to create text files with the code.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2016 09:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252692#M48009</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-26T09:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create a tempory file [Windows]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252710#M48023</link>
      <description>&lt;P&gt;Just to be clearer:&amp;nbsp; The macro &lt;STRONG&gt;does&lt;/STRONG&gt; use &lt;FONT face="courier new,courier"&gt;filename ... temp ...&lt;/FONT&gt; in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   %if %sysevalf(&amp;amp;sysver  &amp;gt; 6.10) %then %do;
		filename &amp;amp;fileref temp &amp;amp;options
			%if %length(&amp;amp;ls)&amp;gt;0 %then lrecl=&amp;amp;ls;
			;		
		%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A typical use for this is in my table macro, &lt;A href="http://www.datavis.ca/sasmac/table.html" target="_self"&gt;http://www.datavis.ca/sasmac/table.html&lt;/A&gt; where I convert numeric variables to their formatted character strings by printing them to a temporary file and then reading the result back in.&amp;nbsp; (Maybe there is now&lt;/P&gt;
&lt;P&gt;an easier way to do this??).&amp;nbsp; The code for this is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;char)&amp;gt;0 %then %do;
   /*
    * Force the VAR= variables to character.  To do this cleanly, we
    * resort to printing the &amp;amp;out dataset, then reading it back as
    * character. 
    * In SAS 9.3, this only works if ODS LISTING is turned on. 
    */
   %tempfile(table,&amp;amp;ls);
   %if %sysevalf(&amp;amp;sysver &amp;gt;9.2) %then %do;
   	ods listing;
   %end;
   proc printto new print=table;
   options nodate nocenter nonumber ls=&amp;amp;ls ps=10000;
   proc print data=&amp;amp;out;
      id &amp;amp;var;
      var count;
      run;
   %if &amp;amp;syserr &amp;gt; 4 %then %let abort=1; %if &amp;amp;abort %then %goto DONE;
   proc printto print=print;


   %let tvar = %join(&amp;amp;var, $) $;
   %*put tvar=&amp;amp;tvar;

   %if %verify(&amp;amp;char,  %str(0123456789))=0
      %then %let clen=&amp;amp;char;
      %else %let clen=16;
   data &amp;amp;out;
      infile table length=len;
      length string $&amp;amp;ls &amp;amp;var $&amp;amp;clen;
      retain skipping 1;
      drop string skipping;
      input @1 string $varying. len @;
      if skipping=0 &amp;amp; string ^= ' ' then do;
         input @1 &amp;amp;tvar count;
         output;
         end;
      else input;
      if index(string,'COUNT')&amp;gt;0 then skipping=0;
   run;
   *proc contents data=&amp;amp;out;

%tempdel(table);
 %if %sysevalf(&amp;amp;sysver &amp;gt;9.2) %then %do;
 	ods listing close;
 %end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2016 14:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-a-tempory-file-Windows/m-p/252710#M48023</guid>
      <dc:creator>michael_friendly</dc:creator>
      <dc:date>2016-02-26T14:17:10Z</dc:date>
    </item>
  </channel>
</rss>

