SAS How To's

How to Create Variables with Spaces or Special Characters in SAS

Like many other programming languages, SAS doesn’t recommend to create variable names that contain spaces or special characters. However, this doesn’t mean you can’t. In this article, we discuss how to create SAS variable names that have spaces and special characters.

In fact, a SAS variable can contain spaces and/or special characters. You define a variable name that contains spaces or special characters by writing the name between single quotes (‘) followed by the character n, for example My new Variable'n. Make sure that you use the global SAS-option validvarname=any, before you run your code.

Below follow some examples of how to create SAS variables with spaces and/or special characters, and how to use them. However, before we continue, we need to discuss the VALIDVARNAME=-option first.

The VALIDVARNAME Option

What is the VALIDVARNAME Option?

The VALIDVARNAME=-option is a global SAS option that controls the type of variable names that can be used and created in a SAS session.

By default, when you start a new SAS session, the VALIDVARNAME=-option is set to ‘V7’. This means that column names can contain only alphanumeric characters and underscores. Hence, you must change this option if you want to create variable names with spaces or special characters.

How to Change the VALIDVARNAME Option?

You use the OPTION statement and validvarname=any to allow SAS to create column names with spaces and special characters. This line of code should be the first code you write in your SAS program.

option validvarname=any;

How to Check the Current Value of the VALIDVARNAME Option?

You use the PROC OPTIONS statement to check the current value of the VALIDVARNAME option. For example:

proc options option=VALIDVARNAME short;
run;
SAS VALIDVARNAME Option

How to Create a SAS Variable with a Space or Special Character

Once you have set the option validvarname=any, you can create variable names with spaces or special characters. To do so, you write the name of the variable between single quotes followed by the letter n.

In the example below, we show how to create the numeric variable My Variable One with a Data Step.

option validvarname=any;
 
data work.my_ds;
    input 'My Variable One'n;
    datalines;
1
2
3
;
run;

If you want to learn more about how to create a dataset with manual input, this article might be of interest for you.

Instead of using the DATALINES statement, you can also create a new variable using an existing dataset.

Here, we use a SAS Data Step to create the new variable Weight in KG using the Cars dataset from the SASHELP library as input. The Cars dataset contains the Weight of each vehicle in Pounds. We multiply this column by 0.45 to get the weight in Kilograms.

option validvarname=any;
 
data work.my_ds;
    set sashelp.cars (keep=Make Model Weight);
 
    'Weight in KG'n = Weight * 0.45;
run;
SAS Column name with Spaces

As you can see above, we used to KEEP=-option to select 3 columns and we have created a new variable called Weight in KG.

Read this article to learn more how to create new variables in SAS, as well as many useful SAS functions.

You can also use PROC SQL to create a new variable with a space in its name. In the example below, we rename the variable Weight to Weight in LBS, and create the new variable Weight in KG. In both cases, we place the new variable name between single quotes followed by the letter n.

option validvarname=any;
 
proc sql;
    create table work.cars_weight_in_kg as
        select Make,
	    Model,
	    Weight as 'Weight in LBS'n,
	    Weight * 0.45 as 'Weight in KG'n
	from sashelp.cars;
quit;
SAS column names with spaces

How to Create a New SAS Variable Based on a Variable with a Space or Special Character

So far, we’ve demonstrated how to use the VALIDVARNAME=-option and how to create a SAS variable with spaces in its name. You can also use variables with spaces in their name to create new variables. To do so, you need to put the variable name between single quotes and the letter n.

In the example below, we us the variable Weight in KG to create the new variable Weight in Stones. We place Weight in KG between single quotes followed by the letter n, and multiply it by 0.157.

option validvarname=any;
 
proc sql;
    create table work.cars_weight_in_stones as
        select Make,
	    Model,
	    'Weight in LBS'n,
	    'WEIGHT IN KG'n,
	    'Weight in KG'n * 0.157 as 'Weight in Stones'n
	from work.cars_weight_in_kg;
quit;

How to Rename a SAS Variable with a Space or Special Character

Frequently you work with datasets that are prepared by others. If you don’t like the original variable names, you can change them with the RENAME=-option. Here, we use the RENAME=-option to change the original column names into column names with spaces and special characters. Again, the new column names must be placed between single quotes and the letter n.

option validvarname=any;
 
data work.ds_renamed 
    (rename=(Invoice = 'Invoice in $'n
        Weight = 'Weight in LBS'n
	MPG_City = 'M/G City'n));
    set sashelp.cars (keep=Model Invoice Weight MPG_City);
run;

For more information and examples about the powerful RENAME=-option, check out this article.

How to Sort a Dataset by a Variable with a Space or Special Character

Finally, you can use SAS variables with spaces in their names to sort a dataset. You need the PROC SORT procedure (or the ORDE BY statement in PROC SQL) to order your dataset. The variable that you use to sort (and contains spaces) needs to be placed between single quotes followed by the letter n.

In the example below, we order a dataset set based on the column Weight in LBS.

option validvarname=any;
 
proc sort data=work.ds_renamed
    out=work.ds_sorted;
    by 'Weight in LBS'n;
run;

One thought on “How to Create Variables with Spaces or Special Characters in SAS

Comments are closed.