BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MadQuidd
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11
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;

View solution in original post

7 REPLIES 7
mohamed_zaki
Barite | Level 11

 

To get an appropriate answer...
Give example of the data set you have... and how you want your output to look like

MadQuidd
Obsidian | Level 7

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;

Reeza
Super User

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;
MadQuidd
Obsidian | Level 7

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.

 

 

mohamed_zaki
Barite | Level 11
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;
mohamed_zaki
Barite | Level 11

@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 

IF-THEN/ELSE Statement

Ways to Create Variables

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 882 views
  • 3 likes
  • 3 in conversation