SAS : Variable Name having Spaces or Special Characters

Deepanshu Bhalla 13 Comments

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.

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.

Sample Dataset

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;
Rename the variable 'var1' to 'variable one'

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.

SAS : Variable Name having Spaces or Special Characters
Can variable name starts with a number?

Yes, follow the code below -


options validvarname=any;
data temp2;
set temp;
rename var1 = '1variable'n;
run;
How to read a dataset with spaces in its name?

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;
Related Posts
Spread the Word!
Share
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 worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 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.

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

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

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

    ReplyDelete
  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;
    Run;

    ReplyDelete
  5. Thank you! This was a great help!

    ReplyDelete
  6. great it works for me exporting with proc json

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

    thanks

    ReplyDelete
  7. variable name is " Number of Stores" .I am unable to subset it using where and if.
    Syntax 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.

    ReplyDelete
  8. Hi,

    There 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

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

      Delete
  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
    [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

    ReplyDelete
Next → ← Prev