stringsastext-parsing

passing a list of quoted strings as a parameter to a SAS macro and using it in a data field


I am writing a macro to send e-mails, using a rest api available in our company. (The macro will be deployed as a sasautos macro.) The attachments parameter should allow to pass a list of file paths, so this should be valid code;

%send_mail(subject=Voorbeeld e-mail
    , to=dirk.horsten@ourOrganisation.be
    , message_html =Dear collegue<br/><br/>Here are the promised files.<br/><br/>Regards<br/>Dirk Horsten
    , attachments = "/our/data/répertoire français/file&today_yymmdd..xlsx" 
        "/our/data/some_folder/file with blanks.xlsx" 
    );

Within the macro %send_mail(), I would like to put the attachments parameter in a string attachment_list Perl Regular Expressions in SAS.

data _null_;
    ...
    length fileRef $8 xpath $256 ;
    attachment_list = "&attachments";
    _rxAttachment = prxParse('/".+?"/');
    _rxStart = 1;
    _rxStop = length(attachment_list);
    call prxNext(_rxAttachment, _rxStart, _rxStop, attachment_list, _rxPos, _rxLen);
    if not _rxPos then putlog 'WARNING: unable to parse your attachments';
    else do;
        attach_nr = 0;
        do while (_rxPos);
            attach_nr = attach_nr + 1;
            xpath = substr(attachment_list, _rxPos, _rxLen);
            fileRef = cats('_ML_',attach_nr);
            call execute (catx(' '
                , 'filename'
                , fileRef
                , xpath
                , ';'));
            length attachRef_N attachName_N attachType_N $32;
            attachRef_N = cats('attachRef_', attach_nr);                        
            call symputx(attachRef_N, fileRef, 'local');
            attachName_N = cats('attachName_', attach_nr);
            call symputx(attachName_N, scan(xpath, -1, '/'), 'local');
        end;
        call symputx('attach_count', attach_nr, 'local');
    end;
    ...
run;

(I hope I did not make errors while simplifying the code for you.)

However, the quotes within the parameter mess up with the quotes in attachment_list = "&attachments"; Does anyone see a work around, so that my users should not worry about this?

The following are not acceptable solutions:


Solution

  • Maybe you can load the quoted string into an array then you can quote them or otherwise process as needed.

    50         %let attachments = "/our/data/répertoire français""/file/today_yymmdd..xlsx"
    51                 "/our/data/some_folder/file with blanks.xlsx";
    52         %let attw = %sysfunc(countw(%superq(ATTACHMENTS),%str( ),Q));
    53         %put NOTE: &=attw;
    NOTE: ATTW=2
    54         data _null_;
    55            array _att[&attw] $256 (&attachments);
    56            put _all_;
    57            do i = 1 to dim(_att);
    58               _att[i] = quote(strip(_att[i]),"'");
    59               end;
    60            put _all_;
    61            run;
    
    _att1=/our/data/répertoire français"/file/today_yymmdd..xlsx _att2=/our/data/some_folder/file with blanks.xlsx i=. _ERROR_=0 _N_=1
    _att1='/our/data/répertoire français"/file/today_yymmdd..xlsx' _att2='/our/data/some_folder/file with blanks.xlsx' i=3 _ERROR_=0 _N_=1