- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Run a proc contents on the input data set and show that output please.
Or to fix the overall issue:
- Add this line to your AUTOEXEC : options validvarname=v7;
- Restart your SAS session
- Reimport your data and SAS will provide usable names.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Run a proc contents on the input data set and show that output please.
Or to fix the overall issue:
- Add this line to your AUTOEXEC : options validvarname=v7;
- Restart your SAS session
- Reimport your data and SAS will provide usable names.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Interesting approach.
I will definitely try it.
Thank you 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.