Hello folks.
I faced some strange thing today which I have never seen before.
After running a reference code that I have to use, I find out that some variables have numeric variable names (i.e. 1, 2, 3).
I want to make some changes to them but I have no idea how to refer them.
Task needs to be accomplished - Convert values in variables 1, 2, and 3 from numeric to char.
Thank you in advance.
Kind Regards
Artur
Run a proc contents on the input data set and show that output please.
Or to fix the overall issue:
That's a name literal, and it isn't a valid name. I'm assuming you have
options validvarname = any;
specified somewhere.
You have to refer to it like this:
"1"n
options validvarname = any;
data want;
input '1'n;
datalines;
11
11
.
11
11
;
run;
proc means data = want;
var '1'n;
run;
Probably a good idea to rename those to avoid the hassle of name literals.
Someone must have changed your VALIDVARNAME option setting to ANY to allow it to create names like that.
To reference those non-standard names in SAS code you need to use name literals. Add quotes and the letter N to the name.
proc freq;
tables '1'n "2"N ;
run;
Thank you for your response @maguiremq and @Tom , but it doesn't work that way.
I tried to perform three different actions with that variable with a numeric name, but had no success.
Run a proc contents on the input data set and show that output please.
Or to fix the overall issue:
Thank you @Reeza
Added 'options validvarname=v7;' at the top of my program.
Now I have _1, _2, and _3 variables instead of 1, 2, and 3.
Appreciate all your responses guys.
Have a good rest of your day.
Regards
Artur
Most likely you have leading blanks also, which would cause the same effect:
options validvarname=any;
data have;
input " 1"n;
datalines;
1
;
data want;
set have;
one = "1"n;
run;
proc sql noprint;
select nliteral(name) into :names separated by " " from dictionary.columns
where libname = "WORK" and memname = "HAVE";
quit;
%put &names.;
The SQL will display the real names of the variables, which you can then use in further code to rename them.
Interesting approach.
I will definitely try it.
Thank you 🙂
Since you import the data yourself, you can fix it there, which I also recommend. My approach is for situations where you receive a SAS dataset with such (IMO stupid) names and have to proceed from there.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.