Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

SAS : Variable Name having Spaces or Special Characters

$
0
0
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;

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


Viewing all articles
Browse latest Browse all 425

Trending Articles