I am trying a scenario where a data set with variable names can accept all these special characters and these variables pass through macro variable for further processing.
~ ` ! @ # $ % ^ & ( ) _ + = { } [ ] ; ' ,
For example, i created this exhaustive list of all special characters separated by letter 'u'. Is this possible at all?
Also, i am using the VALIDVARNAME=ANY options in SAS.
%let subject=u~u`u!u@u#u$u%u^u&u(u)u_u+u=u{u}u[u]u;u'u,
proc sort data=data1 out=data2 nodupkey;
by &subject;
run;
Instead of having to use complicated programming that is hard to maintain and error-prone, get rid of the non-standard variable names as soon as the data is imported into SAS, and keep the original names in labels. LABELS are the place for fancy strings.
Hi @jins ,
You can use the function %nrstr() to hide special characters. Don't forget to add a % before characters that generally occur by pairs such as ' or ".
Hope this help!
%let subject=%nrstr(u~u`u!u@u#u$u%u^u&u(u)u_u+u=u{u}u[u]u;u%'u,);
Instead of having to use complicated programming that is hard to maintain and error-prone, get rid of the non-standard variable names as soon as the data is imported into SAS, and keep the original names in labels. LABELS are the place for fancy strings.
Hi @jins ,
If you found one of the answers to be the solution for you then please mark it as solution so we know you've got your answer. Marking something as solution will also help others who search the forums later on to get answers to similar problems.
Thanks
Hello,
According to the documentation, with validvarname=any, you are limited to 32 characters anyway
so your example, for instance, could not work.
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000279245.htm
When using an unconventional variable name, you have to quote it as 'var name'n or "var name"n.
Use the latter if you want macrovariables between quotes to be resolved.
Here is an example without characters % and & to avoid dealing with unwanted macro resolutions.
option validvarname=ANY;
data have;
input "@u#u$u^u(u)u{u}u[u]u;u'u,"n;
cards;
1
2
3
0
;
run;
data _NULL_;
call symputx("subject","@u#u$u^u(u)u{u}u[u]u;u'u,");
run;
proc sort data=have out=want;
by "&subject."n;
run;
The best solution being following @Kurt_Bremser's advice and rename your variables to standard names as soon as possible.
Are the strange characters part of the variable NAME or the VALUE?
For a name you need to use name literals.
%let subject= 'a@b'n;
To make it easier use the NLITERAL() function.
call symputx('subject',nliteral(_name_));
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.