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.
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.
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.
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;The FORMAT statement is used to display the change value in percentage format. In this case, we are creating a new dataset as well.
SET Example1;
OldPrice=5 + OldPrice;
NewPrice=OldPrice*2;
Change= ((NewPrice-OldPrice)/ OldPrice);
Format Change Percent10.0;
RUN;
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.
Amazing site ...
ReplyDeletewhy we are creating a new data set called "readin" here??
ReplyDeleteI'm curious as well
DeleteI have added explanation of the reason of creating a new dataset in the article. Hope it helps.
Deletehi Deepanshu Bhalla Sir,
DeleteThanks 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.
learning progressively. Thanks.
ReplyDeleteThanks a lot
ReplyDeleteHi, i tried the above code for "III. Creating or Modifying a variable"
ReplyDeletebut i am getting missing values in oldPrice, newprice and change. why is that so?
Please post your code here. Thanks!
DeleteHi deepanshu
ReplyDeleteexcellent 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
Add LENGTH statement before 'Type = "Good Price";'. For eg. length Type $12;
DeleteIf 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.
DeleteHi my code is..
ReplyDeleteproc 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
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:
Deleteproc import datafile= "/folders/myfolders/sasuser.v94/Emp.xls"
hi
ReplyDeleteCAN U SPECIFY ABOUT 'LABEL' STATEMENT?
Do you provide domain knowledge training also and training on domain specific data sets
ReplyDeleteThere 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
DeleteUseful.
ReplyDeletedata expl3;
ReplyDeleteset 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
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%
DeleteThank you very much. I like your site. Keep it coming!
ReplyDeleteVery helpful site.
ReplyDeletePlease 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.
data one;
Deleteinput id;
datalines;
89
180
2003
39123
;
run;
data two;
set one;
Unique_D135_2018="D135_2018_"||strip(id);
run;
Nice
ReplyDelete