SAS Safe Column Names

家住魔仙堡 提交于 2019-12-01 18:34:15

proc transpose will take those names without any modification, as long as you set options validvarname=any;

If you want to work with the columns afterwards, you can use the NLITERAL function to construct named literals that can be used to refer to them:

options validvarname=any;

/* Create dataset and transpose it */
data zz;
    var1 = "Rob Penridge";    
    var2 = 5;
    output;

    var1 = "$*@'Blah@*";
    var2 = 100;
    output;           
run;

proc transpose
    data = zz
    out  = zz_t;
    id     var1;
run;


/* Refer to the transposed columns in the dataset using NLITERAL */
data _null_;
    set zz;
    call symput(cats("name", _n_), nliteral(var1));
run;

data blah;
    set zz_t;
    &name1. = &name1. + 5;
    &name2. = &name2. + 200;
run;

May try perl regular expression function. Since for column name, the first character should not be numerical, it's more complicated then.

data _null_;
name1 = "1$*@' Blah1@*";
name2 = prxchange("s/[^A-Za-z_]/_/",1,prxchange("s/[^A-Za-z_0-9]/_/",-1,name1));
put name2;
run; 

Take a look at the VALIDVARNAME System Option. It might allow you to accept non-valid SAS names.

Also the NOTNAME function could facilitate in helping find invalid characters.

How about using SAS's regular expression functionality? For example:

data names;
 set name;
 name_cleaned = prxchange('s/[^a-z0-9 ]/_/i', -1, name);
run;

This will convert anything that isn't a letter, number, or space into a _. You can add other characters that you want to allow to the list after the 9. Just be aware that some characters are "special" and must be preceded by a \.

You could also use the IDLABEL statement in the transpose to add labels that match the original values. Then use the VARLABEL function to retrieve the labels and work with them that way.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!