This article explains how variable names in SAS can include spaces, special characters or even start with numbers and how to handle them, along with examples.
If you use teradata or any other database, you would encounter this problem very soon if you have not encountered it yet. Many times, database column contains blanks or special characters. To read them in SAS, we need to know how to read variables having spaces in their names.
It is also required when we transpose our variables and the variable whose values name the transposed variables in the output data set contains special characters.
Let's create a sample SAS dataset that will be used to explain the examples in this tutorial.
data temp;
input var1;
cards;
1
2
;
run;
options validvarname=any;
data temp2;
set temp;
rename var1 = 'variable one'n;
run;
The options validvarname=any; tells SAS to allow you to have variable name begin with or contain spaces, special characters or numbers.
Additionally, we need to put variable name having spaces in quotes followed by the letter n.
Question : If i don't use VALIDVARNAME=ANY option and use only 'variable one'n , how SAS would take it?
Solution : SAS would return an error "variable name is not valid" as SAS by default cannot contain blanks or special characters.
Yes, follow the code below -
options validvarname=any;
data temp2;
set temp;
rename var1 = '1variable'n;
run;
The option VALIDMEMNAME=EXTEND allows you to read or access dataset (table) whose name having spaces or special characters. In addition, we also need to put name of dataset in quotes followed by the letter n.
options VALIDMEMNAME=EXTEND;
proc print data= 'price data'n;
run;
Awesome sir,Excellent site to learn so many new simple and complex sas coding techniques.First time am refereeing this site as my friend suggested and it is more worhful than any other one i have seen..Thank you so much for sharing knowledge.
ReplyDeleteThank you for stopping by my blog. Cheers!
DeleteIs it possible to rename or create dataset having space in between with the help of validmemname=extend?
ReplyDeleteYes, You can also create dataset having spaces using options VALIDMEMNAME=EXTEND;
Deletesir, I have more than 32 characters variable, can i run these variables in SAS
ReplyDeleteHi m trying to change dataset name wid space in it but its throwing error
ReplyDeleteI have used the following statement
Options validmemname=extend;
Proc datsets library=test;
Change test_ds= 'test ds 'n;
Run;
What error you are getting?
DeleteThank you! This was a great help!
ReplyDeletegreat it works for me exporting with proc json
ReplyDeleteEXPORT model_detail (rename=(Make = '@make'n));
thanks
variable name is " Number of Stores" .I am unable to subset it using where and if.
ReplyDeleteSyntax I am using is
data a;
Set sashelp.shoes;
Where number of Stores =13;
Run;
Please help...i try using compress function but it didn't work.
Hi,
ReplyDeleteThere your using variable name not there in dataset shoes, the variable name is store
data a;
set sashelp.shoes;
where stores=13;
run;
then you will get 13 stores will come out
Hi,
Deletewhat if i get the variable name with spaces?how it reads?
I have imported excel file and variable is now like "Subject[SUBJECT]" I am using below code to rename it
ReplyDeleteoptions validvarname=any;
data pdlis07;
set pdlis07;
rename '"Subject
[SUBJECT]"
'n
=var;
run;
But it is not working. Getting warning says-"WARNING: The variable '"Subject[SUBJECT]"'n in the DROP, KEEP, or RENAME list has never been referenced."
Please help