You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAS宏循环中ODS PDF动态设置/取消密码不生效问题咨询

问题根因

SAS的pdfpassword系统选项存在值残留机制:首次赋值密码后,该值不会随pdfsecurity=none的配置自动清空,后续生成PDF时会默认复用残留的密码值,导致无密码配置不生效。

修复方案

仅需要调整两处配置即可解决:

  • 生成无密码PDF的分支中,除设置pdfsecurity=none外,额外显式添加pdfpassword=()主动清空残留的密码值
  • 修正带密码分支的宏变量引用:你已通过PROC SQL将合同号赋值给宏变量nrref,密码参数处直接使用&nrref即可,无需添加两层&避免解析异常
修正后完整可运行代码
data base_of_cust;
input lp check_MAIL $ ID_CONTRACT ID_CUSTOMER;
datalines;
1 ok 123 1234
2 error 124 1235
3 ok 125 1236
4 ok 126 1237
5 error 127 1238
;run;

%macro Create_pdfs(n);
    %do i = 1 %to &n.; /* 循环开始 */
    proc sql noprint;
    select 
    ID_CONTRACT, ID_CUSTOMER, check_MAIL into :nrref, :nrcust, :validmail
    from base_of_cust c where c.lp=&i.;
    Quit;

    /* 若check_MAIL为'ok'则生成带密码的PDF */
    %IF &validmail. = ok %then %do;
    options nodate nonumber nocenter  papersize="A4" orientation=PORTRAIT pdfsecurity=high pdfpassword=(open=&nrref);
    ods pdf file = "xx\email\%sysfunc(today(),ddmmyyp10.)_%CMPRES(&nrref.)%CMPRES(&nrcust.).pdf" 
    STARTPAGE =no bookmarklist = none color=yes 
    style=tfs;
    title;
        ODS escapechar="^";
        ods pdf title="^S={just=l font_size=2pt font_face=Arial}aa";
        ODS PDF text= "^S={just=l font_weight=bold font_size=12pt font_face=Arial}   ";
        ODS PDF text= "^S={just=l font_size=12pt font_face=Arial}fake test text ";
        ODS _ALL_ CLOSE;
    %end;

    /* 若为error则在其他路径生成无密码PDF */
    %IF &validmail. = error %then %do;
    options nodate nonumber nocenter  papersize="A4" orientation=PORTRAIT pdfsecurity=none pdfpassword=();
    ods pdf file = "xx\poczta\%sysfunc(today(),ddmmyyp10.)_%CMPRES(&nrref.)%CMPRES(&nrcust.).pdf" 
    STARTPAGE =no bookmarklist = none color=yes 
    style=tfs;
    title;
        ODS escapechar="^";
        ods pdf title="^S={just=l font_size=2pt font_face=Arial}aa";
        ODS PDF text= "^S={just=l font_weight=bold font_size=12pt font_face=Arial}   ";
        ODS PDF text= "^S={just=l font_size=12pt font_face=Arial}fake test text ";
        ODS _ALL_ CLOSE;
    %end;

    %end; /* 循环结束 */
%mend;
%Create_pdfs(5);
验证说明

修改后运行宏,check_MAIL为ok的PDF会以对应合同号为打开密码,check_MAIL为error的PDF无密码可直接打开,不会再复用前一次的密码配置。

内容的提问来源于stack exchange,提问作者DarkousPl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 17:27:01