Old macro ps from RAND corporation is trying to get SAS to play ping-pong with R. The macro is from a family called Twang, named after R package that gets propensity scores, hence the name ps. However, the variable names are illegal for SAS, for example, table.name is not a good variable name. I cannot even rename it, SAS complains. Workaround? I would like this code to work:
proc sql noprint;
select distinct table.name
into :elements separated by " "
from _baltab
order by table.name desc;
quit;
Many thanks!
So the other variables also have goofy names?
The RENAME statement or RENAME= dataset option can rename a variable name.
data want;
set have;
rename 'table.name'n=table_name;
run;
Must is come to this? I go into the preceding CSV file and manually alter table.name to table_name. Then I may run the remainder of the macro as a separate macro.
proc import datafile="&path/baltab.csv"
out=_baltab
dbms=csv
replace;
getnames=yes;
datarow=2;
guessingrows=MAX;
proc sql noprint;
select distinct table_name
into :elements separated by " "
from _baltab
order by table_name desc;
quit;
* %put &elements;
unw ks.max.ATT es.mean.ATT
Just set VALIDVARNAME option to V7 before running the PROC IMPORT and you will get a dataset with valid SAS variable names instead of names with periods and other invalid characters in them.
So the other variables also have goofy names?
The RENAME statement or RENAME= dataset option can rename a variable name.
data want;
set have;
rename 'table.name'n=table_name;
run;
Log? Actual log contents? And since this apparently involves a macro then run with Options MPRINT.
You do not show exactly what SAS "complains" about. As in what the log says.
I don't think you are providing all the information.
Before you attempted a dataset name literal did you set Options Validvarname=any; ? That is required to reference data set variable name literals.
No complaints with this:
options validvarname=any; data _baltab; 'table.name'n='some garbage'; run; proc sql noprint; select distinct 'table.name'n into :elements separated by " " from _baltab order by 'table.name'n desc; quit; %put elements are: &elements;
And the log:
249 options validvarname=any; 250 data _baltab; 251 'table.name'n='some garbage'; 252 run; NOTE: The data set WORK._BALTAB has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 253 254 proc sql noprint; 255 select distinct 'table.name'n 256 into :elements separated by " " 257 from _baltab 258 order by 'table.name'n desc; 259 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.03 seconds cpu time 0.00 seconds 260 %put elements are: &elements; elements are: some garbage
So pretty much have to assume how ever you attempted to use a name literal was incorrect syntax.
Woops, in the evening it works 🙂
proc import datafile="&path/baltab_dot.csv"
out=_baltab
dbms=csv
replace;
getnames=yes;
datarow=2;
guessingrows=MAX;
data test;
set _baltab;
rename 'table.name'n = table_name;
run;
@ballardw you were right - incorrect syntax:
rename "table.name"/n = table_name;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.