sas7bdat with spaces in var names

2019-07-04 21:55发布

问题:

I've received several SAS dataset files with the .sas7bdat extension. I'm using SAS 9.3 on windows, and the creator of these files was evidently using a different environment and/or software. Many of the files have var names that include spaces and other invalid characters. Even running a proc contents raises an error like this:

ERROR: The value Person ID is not a valid SAS name.

Oddly, SAS Enterprise Guide opens and displays the file without complaining.

How do I efficiently re-name all my bad var names so that I can start running actual programs with these files?

回答1:

In addition to option validvarname=ANY; you also need to reference the variable names as name literals: 'Person ID'n (either single or double quotes are fine).

If you want to efficiently rename all of them, you can do something like this:

options validvarname=any;
data have;
'Hello Var'n = 1;
'Another Var'n = 2;
x=3;
run;

data badvarnames;
set sashelp.vcolumn;
where libname='WORK' and memname='HAVE' and not nvalid(name,'v7');
validname = translate(trim(name),'_',' ');
name = cats("'",name,"'n");
run;

proc sql;
select catx(' ','rename',name,'=',validname,';') into :renamelist
separated by ' ' from badvarnames;
quit;

proc datasets lib=work;
modify have;
&renamelist;
quit;

proc contents data=have;
run;

Depending on other details (such as the possibility that this might create overlapping variable names), you might need to adjust the code for those concerns.



回答2:

I don't think it is a different OS/SAS version. I think that you may need to submit the following

options validvarname = ANY;


标签: sas