SAS : Variable Name having Spaces or Special Characters

This article may be an eye-opener for you if you think a variable name cannot contain blanks or special characters except for the underscore in SAS. In this article, we would learn how we can read a variable whose name having spaces or special characters. Also, how to deal a variable name starts with a number. It also covers the same case with a dataset (table).

Why do we need to have spaces in a variable name?

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 data
data temp;
input var1;

Rename the variable 'var1'  to 'variable one';
options validvarname=any;
data temp2;
set temp;
rename var1 = 'variable one'n;

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.

Q. If i don't use VALIDVARNAME=ANY option and use only 'variable one'n , how SAS would take it?

Sol : SAS would return an error "variable name is not valid" as SAS by default cannot contain blanks or special characters.
SAS : Variable Name having Spaces

Can variable name starts with a number?

Yes, follow the code below -
options validvarname=any;
data temp2;
set temp;
rename var1 = '1variable'n;

How about reading a dataset whose name having spaces?

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 variable in quotes followed by the letter n.
proc print data= 'price data'n;

ListenData Logo
Spread the Word!
Related Posts
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

13 Responses to "SAS : Variable Name having Spaces or Special Characters"
  1. 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.

  2. Is it possible to rename or create dataset having space in between with the help of validmemname=extend?

    1. Yes, You can also create dataset having spaces using options VALIDMEMNAME=EXTEND;

  3. sir, I have more than 32 characters variable, can i run these variables in SAS

  4. Hi m trying to change dataset name wid space in it but its throwing error

    I have used the following statement
    Options validmemname=extend;
    Proc datsets library=test;
    Change test_ds= 'test ds 'n;

  5. Thank you! This was a great help!

  6. great it works for me exporting with proc json

    EXPORT model_detail (rename=(Make = '@make'n));


  7. variable name is " Number of Stores" .I am unable to subset it using where and if.
    Syntax I am using is

    data a;
    Where number of Stores =13;
    Please help...i try using compress function but it didn't work.

  8. Hi,

    There your using variable name not there in dataset shoes, the variable name is store

    data a;
    where stores=13;

    then you will get 13 stores will come out

    1. Hi,
      what if i get the variable name with spaces?how it reads?

  9. I have imported excel file and variable is now like "Subject[SUBJECT]" I am using below code to rename it
    options validvarname=any;
    data pdlis07;
    set pdlis07;
    rename '"Subject

    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


Next → ← Prev
Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.