SAS宏变量,使用逗号将逗号转换为单引号

时间:2015-12-03 05:56:12

标签: sas sas-macro

有人可以帮助我如何使用单引号更新宏变量。

我从数据集中获取数字作为逗号分隔值,如下所示:

2108008080,2108008081,2108888082,2108858090,213856345等 我必须在SQL中传递这些记录中的每一个,其中条件为Phone_numbers(& current_macro_variable)。为此,我必须在每个值周围添加单引号,因为Phone_numbers列是Oracle DB的字符字段。

非常感谢任何帮助。我无法从SAS社区获得太多帮助。

3 个答案:

答案 0 :(得分:4)

使用PROC SQLselect ... into语法:

/* Build list of quoted phone numbers separated by a comma */
proc sql ;
  select cats("'",phone_number,"'") into :PHONELIST separated by ',' 
  from phonedata ;
quit ;

/* Then pass it into your Oracle query... */
proc sql ;
  select vars
  from ora.table
  where phone in(&PHONELIST) ;
quit ;

答案 1 :(得分:3)

您是否尝试过1功能?使用a修饰符生成单引号。使用c修饰符获取所有引用的字词。使用%let x=2108008080, 2108008081, 2108888082, 2108858090, 213856345; %put %sysfunc(catq(1ac,&x)); 修饰符生成逗号作为输出分隔符。

'2108008080','2108008081','2108888082','2108858090','213856345'

产生:

{{1}}

答案 2 :(得分:0)

引用函数应该可以解决问题。 您必须浏览stringLine中的所有单词并添加引号。

%let macroVar = %str(2108008080, 2108008081, 2108888082, 2108858090, 213856345);

data _null_;
    n = count("&macroVar",",");
    length 
        quotedword $2000.;
    quotedword = "";
    do i = 1 to n+1;
        word = scan("&macroVar",i,",");
        y = quote(compress(word));
        quotedword = strip(quotedword) || "," || strip(y);
    end;
    quotedword = substr(quotedword,2); /* removing first comma from beginning */
    call symputx("macroVarUpdated",quotedword);
run;

%put &macroVar;
%put &macroVarUpdated;