SAS : Creating Or Modifying A Variable - ListenData
Maybe your like
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 |
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 |
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

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"
UnknownSeptember 21, 2015 at 6:06 AMAmazing site ...
ReplyDeleteReplies- Reply
AnonymousAugust 11, 2016 at 7:02 AMwhy we are creating a new data set called "readin" here??
ReplyDeleteReplies
dadaDecember 28, 2016 at 10:59 PMI'm curious as well
DeleteReplies- Reply
Deepanshu BhallaDecember 29, 2016 at 1:45 AMI have added explanation of the reason of creating a new dataset in the article. Hope it helps.
DeleteReplies- Reply
BhupinderNovember 9, 2018 at 10:10 PMhi 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
Reply
AnonymousNovember 4, 2016 at 2:22 AMlearning progressively. Thanks.
ReplyDeleteReplies- Reply
UnknownNovember 17, 2016 at 9:40 AMThanks a lot
ReplyDeleteReplies- Reply
AnonymousNovember 23, 2016 at 1:00 PMHi, 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
Deepanshu BhallaDecember 29, 2016 at 1:46 AMPlease post your code here. Thanks!
DeleteReplies- Reply
Reply
UnknownApril 12, 2017 at 11:17 AMHi 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
Deepanshu BhallaApril 12, 2017 at 11:32 AMAdd LENGTH statement before 'Type = "Good Price";'. For eg. length Type $12;
DeleteReplies- Reply
NikhilOctober 9, 2018 at 11:24 PMIf 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
Reply
AnonymousApril 13, 2017 at 12:39 PMHi 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
AnonymousApril 20, 2018 at 3:27 AMYou 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
Reply
UnknownMay 4, 2017 at 8:25 AMhiCAN U SPECIFY ABOUT 'LABEL' STATEMENT?
ReplyDeleteReplies- Reply
UnknownJune 13, 2017 at 12:34 AMDo you provide domain knowledge training also and training on domain specific data sets
ReplyDeleteReplies
Deepanshu BhallaJune 13, 2017 at 3:11 AMThere 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
Reply
UnknownJanuary 6, 2018 at 4:44 PMUseful.
ReplyDeleteReplies- Reply
UnknownJanuary 12, 2018 at 10:20 PMdata 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
UnknownJanuary 23, 2018 at 6:35 AMPlease 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
Reply
AnonymousApril 3, 2018 at 7:42 AMThank you very much. I like your site. Keep it coming!
ReplyDeleteReplies- Reply
Pinkan18May 11, 2018 at 7:46 AMVery 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
ParveenSharmaMay 26, 2018 at 1:50 AMdata one;input id;datalines;89180200339123;run;data two;set one;Unique_D135_2018="D135_2018_"||strip(id);run;
DeleteReplies- Reply
Reply
UnknownMarch 18, 2019 at 7:48 PMNice
ReplyDeleteReplies- Reply
Tag » How To Add Variables In Sas
-
Creating Variables - SAS OnlineDoc, V8
-
Ways To Create Variables - SAS Help Center
-
How Do I Sum Two Variables Into One New ... - SAS Communities
-
Creating New Variables - SPH - Boston University
-
How To Create A New Variable In SAS - SAS Example Code
-
SAS Tutorials: Computing New Variables - LibGuides
-
How To Create New Variables In SAS (With Examples) - - Statology
-
[PDF] Creating New Variables In An Existing Dataset:
-
SAS Creating New Variables In A Dataset - YouTube
-
SAS Data Tutorial - Creating New Variables - YouTube
-
Creating And Recoding Variables In SAS | SAS Learning Modules
-
[PDF] DSCI 325: Handout 3 – Creating And Redefining Variable In SAS
-
14.2 - The DROP= And KEEP= Options | STAT 481 - STAT ONLINE
-
[PDF] Adding One Value To All Records In A SAS® Data Set - Lexjansen


Unknown
Anonymous
Deepanshu Bhalla