Posted By

sastechies on 10/21/10


Tagged

unix FTP macros on filename sas


Versions (?)

SAS Macro to FTP files between PC and Unix or among Unix server Locations...


 / Published in: SAS
 

  1. %macro ftpfile(localfile,localdir,remotefile,remotedir,remotehost,ftpcommand);
  2.  
  3. options comamid=tcp nosymbolgen;
  4. filename sasparm "!mysasfiles\sasparm"; /*location of a file containing the username and password for access */
  5.  
  6. %global rmtuser rmtpw rc;
  7.  
  8. /* read in the username and password and create macrovars rmtuser rmtpw*/
  9. data _null_;
  10. length rmtuser rmtpw $30.;
  11. infile sasparm;
  12. input rmtuser $
  13. rmtpw $
  14. ;
  15. call symput('rmtuser',trim(rmtuser));
  16. call symput('rmtpw',trim(rmtpw));
  17. run;
  18.  
  19. /* Set a filename FTP statement to remote file */
  20. filename rmfile ftp "&remotefile" cd="&remotedir." host="&remotehost." user="&rmtuser." pass="&rmtpw.";
  21.  
  22. %if &ftpcommand = get %then /*i.e. download remotefile to the Local file */
  23. %do;
  24.  
  25. /* Check if the file exists */
  26. %let rc=%sysfunc(fexist(rmfile));
  27. %*****If unable to establish a filename, send error stop;
  28. %if (&rc eq 0)%then
  29. %do;
  30. %put ******Unable to establish FTP connection to file--&rc.;
  31. %put ******Problem with input file: &remotefile.;
  32. %let rc= -1;
  33. %end;
  34. %else
  35. %do;
  36. %put ******Establish FTP connection to file--&remotefile;
  37.  
  38. data _null_;
  39. infile rmfile;
  40. file "&localdir./&localfile.";
  41. input;
  42. put _infile_;
  43. run;
  44.  
  45. %let filrf=lclfile;
  46. %let rc=%sysfunc(filename(filrf,"&localdir./&localfile."));
  47. %end;
  48. %end;
  49. %else %if &ftpcommand = put %then
  50. %do;
  51. /* Check if the file exists */
  52. %let filrf=lclfile;
  53. %let rc=%sysfunc(filename(filrf,"&localdir./&localfile."));
  54. %if &rc ne 0 %then
  55. %do;
  56. %put %sysfunc(sysmsg());
  57. %end;
  58. %else
  59. %do;
  60. data _null_;
  61. infile "&localdir./&localfile.";
  62. file rmfile;
  63. input;
  64. put _infile_;
  65. run;
  66.  
  67. /* Check if the file exists */
  68. %let rc=%sysfunc(fexist(rmfile));
  69. %end;
  70. %end;
  71.  
  72. %put ftp returncode:&rc;
  73.  
  74. /* Abort the job with rc=16 for batch processes...
  75.   Remove this line if you are using it in interactive sas..else your session will be ended
  76. */
  77. %if &rc ne 0 %then %abort return 16;
  78.  
  79. %mend ftpfile;
  80.  
  81. /* FTP test.sas from desktop to test1.sas on unix server */
  82.  
  83. %ftpfile(test.sas,C:\,test1.sas,/data/dev/sastechies/sas,server,put);
  84.  
  85.  
  86. /* FTP test1.sas from Server to test1.sas on Desktop */
  87.  
  88. %ftpfile(test1.sas,C:\,test1.sas,/data/dev/sastechies/sas,server,get);

Report this snippet  

You need to login to post a comment.