SAS : Variable Name having Spaces or Special Characters

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 10% off till Oct 26, 2017
- Batch starts from October 28, 2017

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;
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.

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;
run;

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.
options VALIDMEMNAME=EXTEND;
proc print data= 'price data'n;
run;


SAS Tutorials : 100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

4 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
    Replies
    1. Thank you for stopping by my blog. Cheers!

      Delete
  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

Next → ← Prev