How to Create or Modify a Variable in SAS Programming? - GeeksforGeeks

23 Jul, 2019
This will help you to create or modify a variable. It is common to define a new variable based on the existing variable.
Let’s create a datasetIn the code below, we are creating a dataset named as Example1 which is going to store on WORK(temporary) library. In this dataset, there would be a variable called OldRate which contains a numeric value. The RUN statement is defined to close the dataset program.
DATA Example1;OldRate=42;RUN;
Output:
Creating a numeric variableYou can create variables using the form: variable = expression;Suppose you are asked to create a new variable NewRate, in the existing SAS data set Example1. Both variables are numeric. The variable NewRate is twice of OldRate.DATA Example1;SET Example1;NewRate=3*OldRate;RUN;Output:If you are asked to store a new variable NewRate on a new dataset, you can create it using DATA statement.DATA Readin;SET Example1;NewRate=3*OldRate;RUN;In above case, the dataset READIN was created.Creating a character variableIn the dataset Example1, let’s create a character variable as Type. The character value for the set is set ‘GeeksforGeeks’.The quote marks need to be entered around the character variable.DATA Example1;SET Example1;Type = 'GeeksforGeeks';RUN;Output:Since Type is a character variable, so the value entered should in quotes. It can be either single or double quotes.Creating or Modifying a variableSuppose the value of OldRate is increased by 8 units and you need to calculate the relative change in rate. In this case, we are modifying the existing variable OldRate so we will add 8 to OldRate. later we calculate the percentage change between old and new rate.DATA Readin;SET Example1;OldRate=8 + OldRate;NewRate=OldRate*3;Change= ((NewRate-OldRate)/ OldRate);Format Change Percent10.0;RUN;Output:The FORMAT statement is used to display the changed value in percentage format.My Personal Notes
arrow_drop_upSave
Creating a numeric variableYou can create variables using the form: variable = expression;Suppose you are asked to create a new variable NewRate, in the existing SAS data set Example1. Both variables are numeric. The variable NewRate is twice of OldRate.DATA Example1;SET Example1;NewRate=3*OldRate;RUN;Output:If you are asked to store a new variable NewRate on a new dataset, you can create it using DATA statement.DATA Readin;SET Example1;NewRate=3*OldRate;RUN;In above case, the dataset READIN was created.
You can create variables using the form: variable = expression;
Suppose you are asked to create a new variable NewRate, in the existing SAS data set Example1. Both variables are numeric. The variable NewRate is twice of OldRate.
DATA Example1;SET Example1;NewRate=3*OldRate;RUN;
Output:
If you are asked to store a new variable NewRate on a new dataset, you can create it using DATA statement.
DATA Readin;SET Example1;NewRate=3*OldRate;RUN;
In above case, the dataset READIN was created.
Creating a character variableIn the dataset Example1, let’s create a character variable as Type. The character value for the set is set ‘GeeksforGeeks’.The quote marks need to be entered around the character variable.DATA Example1;SET Example1;Type = 'GeeksforGeeks';RUN;Output:Since Type is a character variable, so the value entered should in quotes. It can be either single or double quotes.
In the dataset Example1, let’s create a character variable as Type. The character value for the set is set ‘GeeksforGeeks’.
The quote marks need to be entered around the character variable.
DATA Example1;SET Example1;Type = 'GeeksforGeeks';RUN;
Output:
Since Type is a character variable, so the value entered should in quotes. It can be either single or double quotes.
Creating or Modifying a variableSuppose the value of OldRate is increased by 8 units and you need to calculate the relative change in rate. In this case, we are modifying the existing variable OldRate so we will add 8 to OldRate. later we calculate the percentage change between old and new rate.DATA Readin;SET Example1;OldRate=8 + OldRate;NewRate=OldRate*3;Change= ((NewRate-OldRate)/ OldRate);Format Change Percent10.0;RUN;Output:The FORMAT statement is used to display the changed value in percentage format.My Personal Notes
arrow_drop_upSave
Suppose the value of OldRate is increased by 8 units and you need to calculate the relative change in rate. In this case, we are modifying the existing variable OldRate so we will add 8 to OldRate. later we calculate the percentage change between old and new rate.
DATA Readin;SET Example1;OldRate=8 + OldRate;NewRate=OldRate*3;Change= ((NewRate-OldRate)/ OldRate);Format Change Percent10.0;RUN;
Output:
The FORMAT statement is used to display the changed value in percentage format.
SAS Programming
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 10 Programming Languages to Learn in 2022
Shallow Copy and Deep Copy in C++
Difference between Shallow and Deep copy of a class
Kotlin Array
C# | Data Types
Advantages and Disadvantages of OOP
Java Swing | JComboBox with examples
Top 10 Fastest Programming Languages
Use of FLAG in programming
Prolog | An Introduction