Search code examples
sassas-macro

How to save attachments in a physical location of an object in SAS


Hi I want to get all attachments from SAS content server of solution EGRC 6.1 policy object and want to save it in a physical location on my server.

This is what I'm doing now.

proc sql noprint;
select BUSINESS_OBJECT_RK 
into: rk saperated by '-'
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and   ATTACHMENT_TYPE_CD ne "LNK";

select FILE_NM 
into: file saperated by '-'
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and   ATTACHMENT_TYPE_CD ne "LNK";

quit;


%macro attachment;

proc sql noprint;
select count(*) 
into: count
from sasoprsk.attachment_l
where BUSINESS_OBJECT_NM eq "POLICY_INST"
and   ATTACHMENT_TYPE_CD ne "LNK";
quit;

%do i = 1 %to &count;

filename out temp;

    %let rk_l=%scan(%bquote(&rk), %bquote(&i) ,%str(-));
    %let file_l=%scan(%bquote(&file), %bquote(&i) ,%str(-));

%put "file &file_l";
%put "http://sasbap.demo.sas.com/SASContentServer/repository/default/sasdav/Products/SASEnterpriseGRC/EnterpriseGRCMidTier6.1/Content/policy/&rk_l/&file_l"; 

proc http 
method="get"
url="http://sasbap.demo.sas.com/SASContentServer/repository/default/sasdav/Products/SASEnterpriseGRC/EnterpriseGRCMidTier6.1/Content/policy/&rk_l/&file_l"
webUserName="sas"
webPassword="Orion123"
out=out;
run;

%end;
%mend;  

%attachment;

I'm saving my attachment files in temp file but I want to save in a physical location as "C drive" inside a folder named as their rk of my objrct with proper extension as file.doc, file.xls or file.jpg etc.

example 
obj_nm        rk      file_nm
POLICY_INST   12      file.xls
POLICY_INST   13      file.doc
POLICY_INST   14      file.gif

I want to put those files as

C:/12/file.xls 
C:/13/file.doc
C:/14/file.gif 

Kindly tell me how can I save my files from SAS content server to a physical location of my server with proper extension.


Solution

  • Simply add the following before your proc http call:

    %mf_mkdir(C:/&rk_l)
    filename out "C:/&rk_l/&file_l";
    

    The source code for mf_mkdir is available in the open source MacroCore library, and is reproduced below:

    /**
      @file
      @brief Creates a directory, including any intermediate directories
      @details Works on windows and unix environments via dcreate function.
    Usage:
    
        %mf_mkdir(/some/path/name)
    
    
      @param dir relative or absolute pathname.  Unquoted.
      @version 9.2
      @copyright GNU GENERAL PUBLIC LICENSE v3
    **/
    
    %macro mf_mkdir(dir
    )/*/STORE SOURCE*/;
    
      %local lastchar child parent;
    
      %let lastchar = %substr(&dir, %length(&dir));
      %if (%bquote(&lastchar) eq %str(:)) %then %do;
        /* Cannot create drive mappings */
        %return;
      %end;
    
      %if (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\)) %then %do;
        /* last char is a slash */
        %if (%length(&dir) eq 1) %then %do;
          /* one single slash - root location is assumed to exist */
          %return;
        %end;
        %else %do;
          /* strip last slash */
          %let dir = %substr(&dir, 1, %length(&dir)-1);
        %end;
      %end;
    
      %if (%sysfunc(fileexist(%bquote(&dir))) = 0) %then %do;
        /* directory does not exist so prepare to create */
        /* first get the childmost directory */
        %let child = %scan(&dir, -1, %str(/\:));
    
        /*
          If child name = path name then there are no parents to create. Else
          they must be recursively scanned.
        */
    
        %if (%length(&dir) gt %length(&child)) %then %do;
           %let parent = %substr(&dir, 1, %length(&dir)-%length(&child));
           %mf_mkdir(&parent);
        %end;
    
        /*
          Now create the directory.  Complain loudly of any errors.
        */
    
        %let dname = %sysfunc(dcreate(&child, &parent));
        %if (%bquote(&dname) eq ) %then %do;
           %put ERROR: could not create &parent\&child;
           %abort cancel;
        %end;
        %else %do;
           %put Directory created:  &dir;
        %end;
      %end;
      /* exit quietly if directory did exist.*/
    %mend;