Selecting Columns

Overview

To specify which column(s) to display in a query, you write a SELECT clause, the first clause in the SELECT statement. After the keyword SELECT, list one or more column names and separate the column names with commas. In the SELECT clause, you can both specify existing columns (columns that are already stored in a table) and create new columns.
The following SELECT clause specifies the columns EmpID, JobCode, Salary, and bonus. The columns EmpID, JobCode, and Salary are existing columns. The column named bonus is a new column.
proc sql;
   select empid,jobcode,salary,
          salary*.06 as bonus
      from sasuser.payrollmaster
      where salary<32000
      order by jobcode;

Creating New Columns

You can create new columns that contain either text or a calculation. New columns appear in output, along with any existing columns that are selected. Keep in mind that new columns exist only for the duration of the query, unless a table or a view is created.
To create a new column, include any valid SAS expression in the SELECT clause list of columns. You can assign a column alias, a name, to a new column by using the keyword AS followed by the name that you would like to use.
Note: A column alias must follow the rules for SAS names.
In the sample PROC SQL query, shown below, an expression is used to calculate the new column: the values of Salary are multiplied by .06. The keyword AS is used to assign the column alias bonus to the new column.
proc sql;
   select empid,jobcode,salary,
          salary*.06 as bonus
      from sasuser.payrollmaster
      where salary<32000
      order by jobcode;
A column alias is useful because it enables you to reference the column elsewhere in the query.
Note: You can learn more about referencing a calculated column from other clauses in Performing Advanced Queries Using PROC SQL.
Also, the column alias appears as a column heading in the output.
The following output shows how the calculated column bonus is displayed. Notice that the column alias bonus appears in lowercase, exactly as it is specified in the SELECT clause.
Output with the Calculated Bonus
In the SELECT clause, you can specify a label for an existing column or a new column. If both a label alias and a column alias are specified for a new column, the label is displayed as the column heading in the output (footnote 1) . If only a column alias is specified, it is important that you specify the column alias exactly as you want it to appear in the output.
Note: You can learn about creating new columns that contain text and about specifying labels for columns in Performing Advanced Queries Using PROC SQL.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.223.160