Sas if in

Sas if in DEFAULT

SAS : WHERE vs. IF Statements


Difference between WHERE and IF conditions

WHERE condition wins against IF condition in the following cases :

1.The WHEREstatement can be used in procedures to subset data while IFstatement cannot be used in procedures.

Look at the log of WHERE and IF statements shown below :





2.WHERE can be used as a data set option while IF cannot be used as a data set option.

Look at the log of WHERE and IF conditions shown below :





3.The WHEREstatement is more efficient than IF statement. It tells SAS not to read all observations from the data set.

Look at the LOG (shown below) after using WHEREstatement,you only see a count of the number of observations that meet the criteria in the WHERE statement.

Only 6 observations were read from the dataset READIN. In actual, the dataset READIN contains 14 observations.



All 14 observations are read and the 6 that meet the IFcriteria are placed in the new data set.


NOTE : Both statements produced the same result.
The where clause sends only those records that meet condition to PDV, the IF statement sends all the records to PDV and removes the records that do not meet condition before they get sent to the output buffer.

4.The WHEREstatement can be used to search for all similar character values that sound alike while IFstatement cannot be used.

For example, you want to filter out all the names that sound alike 'Sam'.



IF condition wins against WHERE condition in the following cases :

1.When reading data using INPUT statement.

IF Statement

IF Statement can be used when specifying an INPUT statement.







WHERE Statement

WHERE statement can not be used when specifying an input statement.





2.When it is required to execute multiple conditional statements

Suppose, you have data for college students’ mathematics scores. You want to rate them on the basis of their scores.

Conditions :
1.If a score is less than 40, create a new variable named “Rating” and give “Poor” rating to these students.
2.If a score is greater than or equal to 40 but less than 75, give “Average” rating to these students.
3.If a score is greater than or equal to 75 but less than or equal to 100, give “Excellent” rating to these students.

This can be easily done using IF-THEN-ELSE IF statements. However, WHERE statement requires variables to exist in the data set.


3. When it is required to use newly created variables in data set.

IF statement can be applied on a newly created variable whereas WHERE statement cannot be applied on a newly created variable. In the below example, IF statement doesn't require variables to exist in the READIN data set while WHERE statement requires variable to exist in the data set.



4. When to Use _N_, FIRST., LAST. Variables

WHERE statementcannot be applied on automatic variables such as _N_, First., Last. Variables. While IF statementcan be applied on automatic variables.
Difference between IF and WHERE Conditions

Difference : WHERE and IF when merging data sets
WHERE statement applies the subset condition before merging the data sets, Whereas, IF statement applies the subset condition after merging the data sets.

Create 2 Sample Datasets for Merging
data ex1;
input ID Score;
cards;
1 25
2 28
3 35
4 45
;
run;
data ex2;
input ID Score;
cards;
1 95
2 97
;
run;

Merge with WHERE Condition
data comb;
merge ex1 ex2;
by ID;
where score <= 30;
run;
It returns 2 observations. WHERE condition applied before merging. It applies separately on each of the 2 data sets before merging.

Merge with IF Condition
data comb;
merge ex1 ex2;
by ID;
if score <= 30;
run;
It returns 0 observation as IF condition applied after merging. Since there is no observation in which value of score is less than or equal to 30, it returns zero observation.
Sours: https://www.listendata.com/2013/09/sas-where-vs-if-statements.html

Creating New Variables Using if-then;if-then-else; and if-then-else-then Statements


An if-then statement can be used to create a new variable for a selected subset of the observations.

For each observation in the data set, SAS evaluates the expression following the if. When the expression is true, the statement following then is executed.

 

Example:

 

if age ge 65 then older=1;

 

When the expression is false, SAS ignores the statement following then. For a person whose age is less than 65, the variable older will be missing.

Note that the above statement could equivalently be written

 

if age >= 65 then older=1;

 

An optional else statement can be included (if-then-else) to provide an alternative action when the if expression is false.

 

if age ge 65 then older=1;

else older=0;

 

For a person whose age is less than 65, the variable older will equal 0.

An optional else-if statement can follow the if-then statement. SAS evaluates the expression in the else-if statement only when the previous expression is false. else-if statements are useful when forming mutually exclusive groups. 

 

if 40 < age <= 50 then agegroup=1;

else if 50 < age <= 60 then agegroup=2;

else if age > 60 then agegroup=3;

  • A person whose age is between 40 and 50 (notice the strict inequality: those aged exactly 40 will not be included) will be in agegroup 1.
  • A person aged between 50 and 60 will be in agegroup 2 (again, notice the strict inequality: those aged exactly 50 will not be included in this agegroup, but will be in agegroup 1).
  • A person whose age is greater than 60 will be in agegroup 3.
  • A person whose age is 40 or younger will not be assigned to an agegroup, and their agegroup variable will be missing.

Note that this if-then-else-if statement could equivalently be written

if 40 lt age le 50 then agegroup=1;

else if 50 lt age le 60 then agegroup=2;

else if age gt 60 then agegroup=3;

 

An if statement can be followed by exactly one else statement or by many else-if statements. SAS will keep evaluating the if-then-else-if statements until it encounters the first true statement. 

Character variable data must always be enclosed in quotes.

 

 

The following code creates a new variable called group from an existing variable called gpa. The new variable called group takes on one of two values: "good standing" if a person's gpa is greater than or equal to 3.0 and "not good standing" if a person's gpa is less than 3.0.

  

data grades;

inputname $ gpa;

ifgpa<3.0thengroup ="not good standing";

ifgpa>=3.0thengroup ="good standing";

cards;

Ann 3.7

Bart 2.9

Cecil 3.5

Denise 4.0

Emily 2.5

Frank 3.6

;

run;

proc print;

run;

 This results in:

 

 

Note that SAS does not generally distinguish between upper and lower case (you can use either). The exception is in the value of character variables. The value "Good standing" is not the same as the value "good standing".

 

SAS code follows the rules of logic: SAS evaluates if-then statements in the order in which they appear in the datastep.

 

Suppose we want to create a variable called gpagroup which takes on one of 3 values:

  • "Excellent Grades" for those with a gpa greater than or equal to 3.5,
  • "Good" for those with a gpa greater than or equal to 3.0 and
  • "Satisfactory" for those with a gpa greater than or equal to 2.5.

We run the following code:

data grades;

inputname $ gpa;

ifgpa>=3.5thengpagroup ="Excellent Grades";

ifgpa>=3.0thengpagroup ="Good";

ifgpa >=2.5thengpagroup ="Satisfactory";

cards;

Ann 3.7

Bart 2.9

Cecil 3.5

Denise 4.0

Emily 2.5

Frank 3.6

;

run;

 

What went wrong?

Answer

We should instead use if-then-else statements as follows:

 

data grades;

inputname $ gpa;

ifgpa>=3.5thengpagroup ="Excellent Grades";

else ifgpa>=3.0thengpagroup ="Good";

else ifgpa >=2.5thengpagroup ="Satisfactory";

cards;

Ann 3.7

Bart 2.9

Cecil 3.5

Denise 4.0

Emily 2.5

Frank 3.6

;

run;

proc print;

run;

 


return to top | previous page | next page

Sours: https://sphweb.bumc.bu.edu/otlt/mph-modules/bs/sas/sas-basics1/sas-basics16.html
  1. 2012 dodge charger length
  2. Metamorphic protein
  3. Eau de quartz
  4. Inserting pages in indesign
  5. Bsn salary new orleans

Using the IN Operator with the Macro Language in SAS 9.2

In data step code it is common practice to use an IN operator in place of lots of OR statements, this allows you to write code that is concise and less repetitive. In addition the code also runs more efficiently as SAS no longer needs to make multiple evaluations.

data subset; set sashelp.class; *written in full; if name='Alice' or name='James' or name='John' or name='Judy'; *abbreviated using the IN operator; if name in('Alice','James','John','Judy'); run;
proc print data=subset; run;

However if you try to apply the same technique within the macro language then you find that the IN operator is not available:

*macro IN operator; %macro inoperator(proc,ds); %if %upcase(&proc) in (MEANS,PRINT) %then %do; proc &proc data=&ds; run; %end; %mend inoperator; %inoperator(print,sashelp.class);

Attempting to execute this macro will result in an error message as well as the macro being terminated early.

19 %inoperator(print,sashelp.class); ERROR: Required operator not found in expression: %upcase(&proc) in (MEANS,PRINT) ERROR: The macro INOPERATOR will stop executing.

Version 9.2 of SAS now allows you to use an IN operator within the macro language.

In order to use the IN operator, two SAS options need to be considered:

  • MINOPERATOR: This is the most important of the two options as, when set, this allows the IN operator to be used.
  • MINDELIMITER=: Defines a single character to be used as a delimiter for the list of values used in the IN operator. By default this option is set to a single space.
*macro IN operator; options minoperator mindelimiter=','; %macro inoperator(proc,ds); %if %upcase(&proc) in (MEANS,PRINT) %then %do; proc &proc data=&ds; run; %end; %mend inoperator; %inoperator(print,sashelp.class);

In the above example the MINDELIMITER option has been set to a comma, this allows the values to be separated by commas as they are in the data step version of the IN operator.

Invoking the macro now gives the desired result, as the value of &proc is now evaluated against the list of values.

Consultant Ann White also demonstrates techniques for using the macro IN operator in SAS 9.2.:

Posted in SAS Programming; Tagged SAS Programming, IN Operator; Posted January, 2017 by Amadeus Technical Team

Sours: https://amadeus.co.uk/
SAS If Then Else statements

.

If in sas

.

Session 19- If-Then-Else Statement in SAS

.

You will also be interested:

.



434 435 436 437 438