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

如何从杂乱SAS文本中提取表格编号并加15后返回

解决方案:给SAS表格序号批量加15

这里有两种实用方法帮你完成需求,既能保证Tab后的章节编号不变,又能轻松把序号加15,其中一种完全不用PRX正则函数,适合不同场景:

方法一:使用PRX正则快速匹配替换

如果对正则语法比较熟悉,这个方法更灵活,即使序号位数变化(比如从9变成24)也能自动适配:

data _null_;
    infile datalines4 truncover;
    input line $char300.;
    
    /* 替换变量名中的序号(比如Tit_Tab5_15里的15) */
    if prxmatch('/Tit_Tab(\d+)_(\d+)/', line) then do;
        chap_num = input(prxposn(prxparse('/Tit_Tab(\d+)_(\d+)/'), 1, line), 8.);
        tab_num = input(prxposn(prxparse('/Tit_Tab(\d+)_(\d+)/'), 2, line), 8.);
        new_tab_num = tab_num + 15;
        line = prxchange("s/Tit_Tab&chap_num._&tab_num./Tit_Tab&chap_num._&new_tab_num./", 1, line);
    end;
    
    /* 替换字符串中的序号(比如Tab5-15里的15) */
    if prxmatch('/Tab(\d+)-(\d+)/', line) then do;
        chap_num = input(prxposn(prxparse('/Tab(\d+)-(\d+)/'), 1, line), 8.);
        tab_num = input(prxposn(prxparse('/Tab(\d+)-(\d+)/'), 2, line), 8.);
        new_tab_num = tab_num + 15;
        line = prxchange("s/Tab&chap_num.-&tab_num./Tab&chap_num.-&new_tab_num./", 1, line);
    end;
    
    /* 输出修改后的语句 */
    put line;
datalines4;
%let Tit_Tab5_15 =%NRSTR(Tab5-15 Cross-tabulation of blood routine results(SS) );
%let Tit_Tab5_16 =%NRSTR(Tab5-16 Cross-tabulation of urine routine results(SS) );
%let Tit_Tab5_17 =%NRSTR(Tab5-17 Cross-tabulation of blood chemistry results(SS) );
%let Tit_Tab5_18 =%NRSTR(Tab5-18 Cross-tabulation of electrolyte results(SS) );
%let Tit_Tab5_19 =%NRSTR(Tab5-19 Cross-tabulation of coagulation results(SS) );
%let Tit_Tab5_20 =%NRSTR(Tab5-20 Cross-tabulation of blood lipid results(SS) );
;;;;
run;

代码说明:

  • 用prxmatch匹配变量名和字符串中的编号模式
  • prxposn提取章节号(比如Tab5的5)和需要修改的序号
  • 序号加15后,用prxchange完成替换,确保章节号完全保留

方法二:纯SUBSTR+INDEX组合(无需正则)

如果对正则不太熟悉,这个方法通过定位关键符号的位置来截取和替换,逻辑更直观:

data _null_;
    infile datalines4 truncover;
    input line $char300.;
    
    /* 处理变量名中的序号:Tit_Tab5_15 = ... */
    underscore_pos = index(line, '_', 8); /* 找到第二个下划线的位置 */
    eq_pos = index(line, '='); /* 找到等号的位置 */
    tab_num = input(substr(line, underscore_pos+1, eq_pos - underscore_pos -1), 8.);
    new_tab_num = tab_num + 15;
    line = substr(line, 1, underscore_pos) || strip(put(new_tab_num, 8.)) || substr(line, eq_pos);
    
    /* 处理字符串中的序号:Tab5-15 ... */
    tab_pos = index(line, 'Tab');
    dash_pos = index(line, '-', tab_pos); /* 找到Tab后的破折号 */
    space_pos = index(line, ' ', dash_pos); /* 找到序号后的空格 */
    tab_str_num = input(substr(line, dash_pos+1, space_pos - dash_pos -1), 8.);
    new_tab_str_num = tab_str_num + 15;
    line = substr(line, 1, dash_pos) || strip(put(new_tab_str_num, 8.)) || substr(line, space_pos);
    
    /* 输出结果 */
    put line;
datalines4;
%let Tit_Tab5_15 =%NRSTR(Tab5-15 Cross-tabulation of blood routine results(SS) );
%let Tit_Tab5_16 =%NRSTR(Tab5-16 Cross-tabulation of urine routine results(SS) );
%let Tit_Tab5_17 =%NRSTR(Tab5-17 Cross-tabulation of blood chemistry results(SS) );
%let Tit_Tab5_18 =%NRSTR(Tab5-18 Cross-tabulation of electrolyte results(SS) );
%let Tit_Tab5_19 =%NRSTR(Tab5-19 Cross-tabulation of coagulation results(SS) );
%let Tit_Tab5_20 =%NRSTR(Tab5-20 Cross-tabulation of blood lipid results(SS) );
;;;;
run;

代码说明:

  • 用index定位下划线、等号、破折号等关键标记的位置
  • substr截取需要修改的序号,转成数值加15后再转回字符串
  • 通过字符串拼接完成替换,全程不用正则,新手也能轻松理解

两种方法运行后都能得到你需要的输出结果,你可以根据自己的习惯选择使用~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:15:58