SAS : Label Statement & Rename Statement

A LABEL statement to assign a descriptive label to a variable. The LABEL statement can be placed either in a DATA step or directly in the PRINT procedure. When you placed the LABEL statement in a DATA step, the label gets permanently affixed to the variable .When you use LABEL statement in PRINT procedure , the label is available for use only in the PRINT procedure . The maximum length of label is 256 characters or bytes.

The syntax of LABEL statement is :

Label variable_name = “descriptive label”;

variable_name – name of variable

descriptive label – give descriptive name to the variable

First Label Keyword , the variable name , an equals sign(=) and finally a descriptive label in quotation marks. Then , we close the Label statement with semicolon(;) .

We create a temporary dataset to store ID and Scores. We used label statement to give label to ID variable .

Data new;

input ID Score1 $ ;

label ID=”Student ID”;

datalines;

1 45

2 78

3 89

4 99

5 54

;

run;

Output:

We can check property of ID variable by click on ID variable in Columns window . It shows the Label as Student ID , Name of variable as ID , Length of variable as 8 bytes and Type of variable is Numeric. It shows Format and Informat as blanks . We have not defined any format and informat of ID variable.

We create a temporary dataset “test_scores” to store ID  and scores of students. We used input statement to declare variable names ID , Score1 , Score2 and Score3. We have used label statement to give description of particular variable. I assign label to ID variable as Student ID. The label of Score1 is Math Score . The label assign to Score2 is Science Score and label of Score3 is English Score.

Data test_scores ;

length ID $ 3;

/* Score1-Score3 will create variable names as Score1, Score2 and Score3 */

input ID $ Score1-Score3;

/*Multiple label statements  */

label ID = ‘Student ID’

      Score1 = ‘Math Score’

      Score2 = ‘Science Score’

      Score3 = ‘English Score’;

datalines;

1 90 95 98

2 78 77 75

3 88 91 92

;

Output:

We can see Label associated with each variable by click on that variable. For example, I want to check label of ID variable .

 I will click on ID variable under columns window .When I clicked on ID variable , the property window shows Label and other properties of variable as :

It shows Label as Student ID , Name of variable as ID , Length is 3 bytes and Type of variable is Char(character ) . The Format and Informat property is empty as we have not assigned any format with the variable.

Similarly , we can check label associated with Score1 by click o Score1 . It shows Label as Math Score , Name as Score1 , Length as 8 bytes and Type as Numeric .

 

We can also assign labels to multiple variables by using multiple label statements.

Data test_scores ;

length ID $ 3 Name $ 15;

/* Score1-Score3 will create variable names as Score1, Score2 and Score3 */

input ID $ Score1-Score3;

/*Multiple label statements  */

label ID = ‘Student ID’;

label Score1 = ‘Math Score’;

label Score2 = ‘Science Score’;

label Score3 = ‘English Score’;

datalines;

1 90 95 98

2 78 77 75

3 88 91 92

;

run;

Output:

RENAME statement

The Rename statement is used to change the name of a variable .

The syntax of Rename statement is:

Rename variable_name = new_name;

variable_name – The name of a variable

new_name – The changed name of a variable

We create a temporary dataset “test_scores” and rename ID variable as student_id .

Data test_scores ;

length ID $ 3;

input ID $ Score1-Score3;

rename ID = student_id;

datalines;

1 90 95 98

2 78 77 75

3 88 91 92

;

run;

Output:

 

We can also rename multiple variables ID and Score1 as:

Data test_scores ;

length ID $ 3;

input ID $ Score1-Score3;

rename ID = student_id

       Score1= math_score;

datalines;

1 90 95 98

2 78 77 75

3 88 91 92

;

run;

Output:

We can also used multiple rename statements as :

Data test_scores ;

length ID $ 3 Name $ 15;

input ID $ Score1-Score3;

rename ID = student_id;

rename Score1= math_score;

datalines;

1 90 95 98

2 78 77 75

3 88 91 92

;

Output:

SET statement

We can read stored dataset by using SET statement . It is used to copy dataset and stored in new dataset.

For example , I create a new dataset based on old dataset .

data new ;

set test_scores;

run;

Output:

It stores test_scores dataset in “new” dataset.

We create a temporary dataset “abc” as:

data abc;

input id salary;

format salary dollar6.;

datalines;

102 4521

103 4875

;

run;

We read “abc” dataset and stored it in “next” dataset as :

data next;

set abc;

run;

Output:

We can also rename variables in SET statement by using RENAME=  option .

The format of the RENAME= option is :

RENAME = (old1=new1 old2=new2 );

where old1 , old2 are the variable names as they appear in the data set that precedes the RENAME= option , and new1 , new2 are the corresponding new variable names .

The RENAME= options effect on where it appears :

  • If the RENAME= option appears         in the SET statement , then the new variable name takes effect when the program data vector is created . All programming statements within the DATA step must refer to the new variable name .
  • If the RENAME= option appears in the DATA statement , then the new variable name takes effect only when the data are written to the SAS data set. All programming statements within the DATA step must refer to the old variable name.

data new;

set abc(rename=(id=student_id) );

run;

Output:

The syntax in a DATA statement :

DATA dsname(RENAME=(o1=n1 o2=n2));

where dsname is the data set name and o1 and o2 are the old variable names , and n1 and n2 are the new variable names.

data new(rename=(id=student_id));

set abc;

run;

Output:

We can rename multiple variables in DATA statement :

data new(rename=(ID=student_id Score1=math_score));

set test_scores;

run;

Output:

We can rename multiple variables in SET statement :

data new;

set test_scores(rename=(ID=student_id Score1=math_score));

run;

Output:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top