I am trying to create a variable based on the parameters below I dont understand what I need to do to create such a variable (i.e. 'if', 'where' statements?
Create a new variable called Improvement where a 1 is any increase of 16 or above, 2 is any increase that is 10 or more but less than 16, and 3 is any increase below 10. Sort the data by Improvement and print the results
The data itself is an uploaded .txt file but the code that I used to uplaod it is as follows.
data problem3b;
infile '/folders/myfolders/homeworkdata/mood.txt'
delimiter=',' dsd;
input Drug pretest Posttest;
Increase = (Posttest - Pretest); /*have to put the variable after the input statement so sas knows what it is using*/
run;
Im just not understanding how I am supposed to create the variable and how to do so. I know that it has to be placed under the input statment but im confused as to carrying out the code to get the results.
data problem3b;
infile '/folders/myfolders/homeworkdata/mood.txt' delimiter=',' dsd;
input Drug pretest Posttest;
Increase = (Posttest - Pretest); /*have to put the variable after the input statement so sas knows what it is using*/
if increase >= 16 then improvemnt = 1;
else if 10 <= increase <16 then improvemnt =2;
else if increase < 10 then improvemnt = 3;
run;
To get an appropriate answer...
Give example of the data set you have... and how you want your output to look like
Here's a youtube video that demonstrates how to create a new column/variable.
https://www.youtube.com/watch?v=d9C3vlPS9t8&index=11&list=PLVBcK_IpFVi9cajJtRel2uBLbtcLz-WIN
Here is an example of what I think I am supposed to do with an 'if' statement but I still dont know how to create the improvment variable.
data problem3b;
infile '/folders/myfolders/homeworkdata/mood.txt'
delimiter=',' dsd;
input Drug pretest Posttest;
Increase = (Posttest - Pretest); /*have to put the variable after the input statement so sas knows what it is using*/
improvement =
if increase >= 16 then improvemnt = 1
else if 10 <increase <16 then improvement =2
else if increase > 10 then improvement = 3
run;
Did you watch the video?
Make sure to end your statements with a semicolon.
What does your log say?
I would recommend doing it in two steps - import data in one and then create variables in another. When beginning at least, helps to keep the processes clear and easier to see where the issues are. See my comments on your code below. Your log probably highlights a good portion of these as well.
data problem3b;
infile '/folders/myfolders/homeworkdata/mood.txt'
delimiter=',' dsd;
input Drug pretest Posttest;
run;
*suggested separation;
data problem3b_improved;
set problem3b;
Increase = (Posttest - Pretest); /*have to put the variable after the input statement so sas knows what it is using*/
improvement = /*what's this line supposed to do?*/
if increase >= 16 then improvemnt = 1 /*make sure variables are spelled correctly and end in semicolon*/
else if 10 <increase <16 then improvement =2 /*Statements need to end in semicolon*/
else if increase > 10 then improvement = 3 /*Statements need to end in semicolon*/
run;
in the code I have to create the variable 'Improvement' it does not exsist in the data set. I do not understand how to create such a variable and make it fall into certain catagories.
data problem3b;
infile '/folders/myfolders/homeworkdata/mood.txt' delimiter=',' dsd;
input Drug pretest Posttest;
Increase = (Posttest - Pretest); /*have to put the variable after the input statement so sas knows what it is using*/
if increase >= 16 then improvemnt = 1;
else if 10 <= increase <16 then improvemnt =2;
else if increase < 10 then improvemnt = 3;
run;
@MadQuidd wrote:
in the code I have to create the variable 'Improvement' it does not exsist in the data set. I do not understand how to create such a variable
What you did in your code is that you created new variable
Increase = (Posttest - Pretest);
and this variable is not exist in the dataset _most probably_.
What happen here is that the new variable "Increase" is created by "Using an Assignment Statement" which is (=). And you did not specify the length or the type of this variable, but what happened is you assigned to it a value. So SAS determines the length of this variable from its first occurrence. And this new variable gets the same type and length as the expression on the right side of the assignment statement.
Comming to the other variable "improvement" that you wanted to create
improvement =
if increase >= 16 then improvemnt = 1
else if 10 <increase <16 then improvement =2
else if increase > 10 then improvement = 3
In your code you have done several sytanx errors as @Reeza mentioned. Building on what we said before and based on that you will create the new variable by the same method. There was no need for your first statment. As the Assignment Statement in each one of your if condition statment would be enough fot creating the new variable you wanted.
@MadQuidd wrote:
in the code I have to create the variable 'Improvement' it does not exsist in the data set. I do not understand how to create such a variable and make it fall into certain catagories.
I assume that what you mean is how to assign value to a variable based on specific conditions. And i think you know enough about that based on your code. But be carefull when you translate your requirements to code. As you also have logic mistakes in writing your if statments.
read more in
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.