BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pink_poodle
Barite | Level 11

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

8 REPLIES 8
pink_poodle
Barite | Level 11

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

Tom
Super User Tom
Super User

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.

pink_poodle
Barite | Level 11
Thank you, but this would also change the names of stat parameters in that
table. I just want to change table.name to table_name.
Tom
Super User Tom
Super User

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;
pink_poodle
Barite | Level 11
I tried a literal. SAS would still complain. It does not like the dot. The funny thing is that csv with table.name variable is mostly for show. I can remove that part of the ps macro altogether.
It is pretty short, and most of the output is produced before.
ballardw
Super User

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.

pink_poodle
Barite | Level 11

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;

 

pink_poodle_0-1701384839897.png

 

pink_poodle
Barite | Level 11

@ballardw you were right - incorrect syntax:

rename "table.name"/n = table_name; 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 630 views
  • 2 likes
  • 3 in conversation