SAS : Creating or Modifying a Variable

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 over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

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:
24 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. hi Deepanshu Bhalla Sir,

      Thanks for such a good explanation and free course of SAS.

      I am practicing from last 2-3 weeks one modification in this program in case if we create a new dataset ReadIn we need to store example1 data as permanent dataset. Please clarify i'm right or not bcz using temp library example 1 dateset program of creating new dataset Readin is not working.

      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
    2. If you don't have same variable in existing data set in Example1 then it'll work (Without adding length statement). Otherwise you have to add length statement as mentioned by @Deepanshu.

      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
    Replies
    1. You must be working on SAS university edition or SAS on demand, you need to specify the shared folder path name because SAS university edition and SAS on demand can't read files present on the physical drive. For example copy and paste the Emp.xls in sasuser.v94 folder and then import it using this proc import statement:
      proc import datafile= "/folders/myfolders/sasuser.v94/Emp.xls"

      Delete
  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
  8. data expl3;
    set expl2;
    change = oldprice - newprice;
    perchange = (change/newprice)*100;
    Format perchange Percent10.5;
    run;


    i have created this variable but not getting proper perchange variable result

    ReplyDelete
    Replies
    1. Please change the variable name change as change1,You might be using the variable value of change from the data of expl2,I changed the variable name and kept the format as 10.5,it worked for me and i got the output as 100%

      Delete
  9. Thank you very much. I like your site. Keep it coming!

    ReplyDelete
  10. Very helpful site.
    Please help me with this:
    Id
    89
    180
    2003
    39123
    I need to create a new var Unique_D135_2018 based on the ID# and D135_2018. So, for ID 89, his new Unique_D135_2018 is D135_2018_89. ID 39123, the new Unique_D135_2018 is D135_2018_39123.
    I am new in SAS world, please help. Thanks, Shirley.

    ReplyDelete
    Replies
    1. data one;
      input id;
      datalines;
      89
      180
      2003
      39123
      ;
      run;
      data two;
      set one;
      Unique_D135_2018="D135_2018_"||strip(id);
      run;

      Delete
  11. This comment has been removed by the author.

    ReplyDelete

Next → ← Prev