SAS : Creating Or Modifying A Variable - ListenData

Menu Go Home » SAS » SAS : Creating or Modifying a Variable Deepanshu Bhalla 24 Comments SAS 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.
Related Posts SAS Tutorials : Top 100 SAS Tutorials Spread the Word! Share Share Tweet About Author:Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

While I love having friends who agree, I only learn from those who don't Post Comment 24 Responses to "SAS : Creating or Modifying a Variable"
  1. UnknownSeptember 21, 2015 at 6:06 AM

    Amazing site ...

    ReplyDeleteReplies
      Reply
  2. AnonymousAugust 11, 2016 at 7:02 AM

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

    ReplyDeleteReplies
    1. dadaDecember 28, 2016 at 10:59 PM

      I'm curious as well

      DeleteReplies
        Reply
    2. Deepanshu BhallaDecember 29, 2016 at 1:45 AM

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

      DeleteReplies
        Reply
    3. BhupinderNovember 9, 2018 at 10:10 PM

      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.

      DeleteReplies
        Reply
    4. Reply
  3. AnonymousNovember 4, 2016 at 2:22 AM

    learning progressively. Thanks.

    ReplyDeleteReplies
      Reply
  4. UnknownNovember 17, 2016 at 9:40 AM

    Thanks a lot

    ReplyDeleteReplies
      Reply
  5. AnonymousNovember 23, 2016 at 1:00 PM

    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?

    ReplyDeleteReplies
    1. Deepanshu BhallaDecember 29, 2016 at 1:46 AM

      Please post your code here. Thanks!

      DeleteReplies
        Reply
    2. Reply
  6. UnknownApril 12, 2017 at 11:17 AM

    Hi deepanshuexcellent explanation very useful information postedmy queryDATA Example1;SET Example1;Type = "Good Price";RUN;above code shows output type=Good onlynew variable default size is 8please suggest how to change default size of new variable

    ReplyDeleteReplies
    1. Deepanshu BhallaApril 12, 2017 at 11:32 AM

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

      DeleteReplies
        Reply
    2. NikhilOctober 9, 2018 at 11:24 PM

      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.

      DeleteReplies
        Reply
    3. Reply
  7. AnonymousApril 13, 2017 at 12:39 PM

    Hi my code is..proc import datafile="C:\SAS-REF\SOURCE DATA\EXCEL\Emp.xls"out=work.dsdbms=xlsreplace;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

    ReplyDeleteReplies
    1. AnonymousApril 20, 2018 at 3:27 AM

      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"

      DeleteReplies
        Reply
    2. Reply
  8. UnknownMay 4, 2017 at 8:25 AM

    hiCAN U SPECIFY ABOUT 'LABEL' STATEMENT?

    ReplyDeleteReplies
      Reply
  9. UnknownJune 13, 2017 at 12:34 AM

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

    ReplyDeleteReplies
    1. Deepanshu BhallaJune 13, 2017 at 3:11 AM

      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

      DeleteReplies
        Reply
    2. Reply
  10. UnknownJanuary 6, 2018 at 4:44 PM

    Useful.

    ReplyDeleteReplies
      Reply
  11. UnknownJanuary 12, 2018 at 10:20 PM

    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

    ReplyDeleteReplies
    1. UnknownJanuary 23, 2018 at 6:35 AM

      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%

      DeleteReplies
        Reply
    2. Reply
  12. AnonymousApril 3, 2018 at 7:42 AM

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

    ReplyDeleteReplies
      Reply
  13. Pinkan18May 11, 2018 at 7:46 AM

    Very helpful site. Please help me with this:Id 89180200339123I 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.

    ReplyDeleteReplies
    1. ParveenSharmaMay 26, 2018 at 1:50 AM

      data one;input id;datalines;89180200339123;run;data two;set one;Unique_D135_2018="D135_2018_"||strip(id);run;

      DeleteReplies
        Reply
    2. Reply
  14. UnknownMarch 18, 2019 at 7:48 PM

    Nice

    ReplyDeleteReplies
      Reply
Add commentLoad more... Next → ← Prev Home Subscribe to: Post Comments (Atom) Follow us on Facebook

Tag » How To Add Variables In Sas