Home > Enterprise >  Creating a character alias in sas, more than 3 words
Creating a character alias in sas, more than 3 words

Time:02-23

I tried to give some of my columns long aliases ( eg. ‘If denied , then give reason’

Proc sql;
Create table xxx as
Sélect 
First name,
Lastsame,
Status as ‘If denied, then give reason’;
QuiT;

I tried the above and got error : ‘Syntax error, expecting a name’ below ‘as’

CodePudding user response:

You have to add a 'n' to the end of the column name:

Status as ‘If denied, then give reason’n;

CodePudding user response:

There are three issues with this, and you have two ways to solve it.

First, your column alias is a string, not a name. SAS (and most any SQL implementation) expects a name - so this:

select status as reason

Not this:

select status as 'reason'

However, if you remove the quotes, that won't work because of the spaces - hence your second issue. You can fix that by adding an n after the quotes - that turns it from a string into a name literal.

select status as 'If denied, then give reason'n

Third, you may not have the ability to use names like this by default, depending on your value of option validvarname. You need to set it to any in order to allow names like this, otherwise only names with no spaces are legal.

options validvarname=any;

In total, what this will look like is:

option validvarname=any;
Proc sql;
  Create table xxx as
    Select 
      Firstname,
      Lastname,
      Status as ‘If denied, then give reason’n;
Quit;

One thing you may want to consider, though, is the concept of label. A label is sort of what you're talking about here - some text to explain a variable - without actually having to use a weird and hard to work with variable name.

Proc sql;
  Create table xxx as
    Select 
      Firstname,
      Lastname,
      Status  ‘If denied, then give reason’;
Quit;

Leaving out the as is all you need - then the label is there. The label will be attached to the variable, and if you proc print with the label option it will print the label instead of the variable name. You still can use the status variable name in code (where it's readable) but the label is attached to the data and easy to use as documentation and for understanding.

Alternately, use underscores (if_denied_then_give_reason); but personally I find status a better variable name and if denied then give reason a good label. This is how we, for example, format our datasets from surveys where I work - the question text becomes the label, and the variable name is a shorter, easy to work with, but still meaningful name.

  • Related