SAS : Creating or Modifying a Variable

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 10% off till Sept 25, 2017
- Batch starts from October 8, 2017

This tutorial demonstrates how to create or modify a variable. It is a common data requirement to create a new variable based on the existing variable. For example, you have existing prices and you need to adjust inflation rate and create new price structure. To achieve it, you need to create a new variable for new prices. Sometimes, you may be asked not to create new variables for changes but adjust the logic in the existing variables.

Let's create a sample dataset

In the code below, we are creating a dataset named Example1 which is stored on WORK (temporary) library. In this dataset, there would be a variable called OldPrice which contains a value 10. The RUN statement is used to close the dataset program. Press F3 or click on submit button to make the code run.
DATA Example1;
OldPrice=10;
RUN;

I. Creating a numeric variable

You create variables using the form:  variable = expression;

Suppose you are asked to create a new variable NewPrice, in the existing SAS data set Example1. Both variables are numeric. The variable NewPrice is twice of OldPrice.
DATA Example1;
SET Example1;
NewPrice=2*OldPrice;
RUN;
SAS : Create a numeric variable 
If you are asked to store a new variable NewPrice on a new dataset, you can create it using DATA statement.
DATA Readin;
SET Example1;
NewPrice=2*OldPrice;
RUN;
In this case, the dataset READIN was created.

II. Creating a character variable

In the same dataset Example1, let's create a character variable say Type. The character value for set is set 'Good'.

The quote marks needs to be entered around the character variable.
DATA Example1;
SET Example1;
Type = 'Good';
RUN;
Since Type is a character variable, it should in quotes. It can be either single or double quotes.


III. Creating or Modifying a variable

Suppose the value of OldPrice is increased by 5 units and you need to calculate the relative change in price. In this case, we are modifying the existing variable OldPrice so we will add 5 to OldPrice. later we calculate the percentage change between old and new price.
DATA Readin;
SET Example1;
OldPrice=5 + OldPrice;
NewPrice=OldPrice*2;
Change= ((NewPrice-OldPrice)/ OldPrice);
Format Change Percent10.0;
RUN;
The FORMAT statement is used to display the change value in percentage format. In this case, we are creating a new dataset as well.
SAS : Modifying a Variable
Important Note
It's a good practice to create a new dataset when you modify the existing variable. It is because input data should not be altered or changed to have a backup of the dataset. In many times, a small mistake of programmer lead to data loss which can have a high-risk consequence.

SAS Tutorials : 100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

14 Responses to "SAS : Creating or Modifying a Variable"

  1. why we are creating a new data set called "readin" here??

    ReplyDelete
    Replies
    1. I have added explanation of the reason of creating a new dataset in the article. Hope it helps.

      Delete
  2. learning progressively. Thanks.

    ReplyDelete
  3. Hi, i tried the above code for "III. Creating or Modifying a variable"
    but i am getting missing values in oldPrice, newprice and change. why is that so?

    ReplyDelete
  4. Hi deepanshu
    excellent explanation very useful information posted

    my query
    DATA Example1;
    SET Example1;
    Type = "Good Price";
    RUN;


    above code shows output type=Good only
    new variable default size is 8
    please suggest how to change default size of new variable

    ReplyDelete
    Replies
    1. Add LENGTH statement before 'Type = "Good Price";'. For eg. length Type $12;

      Delete
  5. Hi my code is..
    proc import datafile="C:\SAS-REF\SOURCE DATA\EXCEL\Emp.xls"
    out=work.ds
    dbms=xls
    replace;
    sheet="sheet1";
    getnames=yes;
    run;
    my error is....
    ERROR: Physical file does not exist, /pbr/biconfig/940/Lev1/SASApp/C:\SAS-REF\SOURCE DATA\EXCEL\Emp.xls.
    how to solve it.Im using SAS on Demond for Acadamics

    ReplyDelete
  6. hi
    CAN U SPECIFY ABOUT 'LABEL' STATEMENT?

    ReplyDelete
  7. Do you provide domain knowledge training also and training on domain specific data sets

    ReplyDelete
    Replies
    1. There is no separate domain knowledge trainings for now. In existing courses offered, I will use banking/HR/Marketing datasets for demonstration. Later people will get a chance to work on live domain specific projects. To know more about the courses, click on this link

      Delete

Next → ← Prev