I've got a problem trying to deal with variable names with special characters (actually ending with +).
I am using options validvarname=any but when I use 'proc freq' or 'put' on these variable names, I can't get them to work because they thinks it's part of an expression/sum.
Also tried doing a workaround where I change the names of the variables, but that's failing too because any function doesn't like the variable name as an input.
Any ideas?
My code is below.
It gets the variable names using proc contents, and uses macro variables to loop through numeric and char variables separately.
The errors come when trying to do the proc freq and put, so basically need to know how to get these functions to read the full variable name without truncating/removing the + from end of variable name.
I am seeking to apply to a dataset I didn't create which has the variable names with + at the end - obviously I wouldn't choose to name my variables like that!!!:
OPTIONS VALIDVARNAME=ANY MLOGIC MPRINT SYMBOLGEN;
data shoes_2;
set sashelp.shoes;
format 'new_var_30+'n 8.;
'new_var_30+'n = 1000;
run;
%macro check_static(LName, DSName);
proc contents data=&LName..&DSName. noprint out=pc(keep=memname name type length nobs);
run;
/* Numeric Variables */
proc sql noprint;
select name into : num_vars separated by " "
from pc
where type = 1;
quit;
proc sql noprint;
select count(*) into : nvars_num
from pc
where type = 1;
quit;
/* Character Variables */
proc sql noprint;
select name into : char_vars separated by " "
from pc
where type = 2;
quit;
proc sql noprint;
select count(*) into : nvars_char
from pc
where type = 2;
quit;
/* Create empty dataset */
proc sql;
create table &DSName._Static
(
Dataset char length 32 format $20.,
Variable char length 32 format $20.,
Is_Static num length 8 format 3.,
Static_Value char length 20 format $20.
);
quit;
/* Loop for numeric variables*/
%do i=1 %to &nvars_num.;
%let var = %scan(&num_vars., &i., ' ');
proc freq noprint data=&LName..&DSName. order=freq;
tables &var. / missing out=pf;
run;
data pf_one;
set pf(obs=1);
Variable = "&var.";
run;
proc sql;
create table num_data as select
a.memname as Dataset format $20.,
a.name as Variable format $20.,
case when b.count=a.nobs then 1 else 0 end as Is_Static format 3.,
case when b.count=a.nobs then put(b.&var.,20.) else "" end as Static_Value format $20.
from pc as a
left join pf_one as b
on a.name = b.variable
where a.name = "&var.";
quit;
proc append base=&DSName._Static
data=num_data force;
run;
%end;
/* Loop for character variables*/
%do j=1 %to &nvars_char.;
%let var = %scan(&char_vars., &j., ' ');
proc freq noprint data=&LName..&DSName. order=freq;
tables &var. / missing out=pf;
run;
data pf_one;
set pf(obs=1);
Variable = "&var.";
run;
proc sql;
create table char_data as select
a.memname as Dataset format $20.,
a.name as Variable format $20.,
case when b.count=a.nobs then 1 else 0 end as Is_Static format 3.,
case when b.count=a.nobs then put(b.&var.,$20.) else "" end as Static_Value format $20.
from pc as a
left join pf_one as b
on a.name = b.variable
where a.name = "&var.";
quit;
proc append base=&DSName._Static
data=char_data force;
run;
%end;
proc sort data=&DSName._Static;
by Variable;
run;
%mend;
%check_static(work, shoes_2);
... View more