In the SAS Programming 2 course, the solution is
if first.Product then TotalProfit=0;
but in the instruction, where was all done with first.variable = 1 or last.variable = 1. Why was it omitted in this cause? Is it because =1 means True and "if first.product " statement means if True as well?
also why is the last.product have a =1 after it?
Is it because if you have a if/then statement you don't need the =1, but you use only the IF statement, you will need to put the =1?
The =1 is not needed because SAS automaticaly evaluates 1 as "true" and 0 as "false". Actually, SAS evaluates any numeric value. 0 and missing values are considered to be false. Any other value (including negative numbers) is considered to be true.
Boolean variables created by SAS (like the FIRST. and LAST., but also those defined with IN= or END= options) can only take on values of 0 and 1, which means false and true, respectively, and can be used as a Boolean value on their own.
The two expressions are equivalent. The first more clearly shows the INTENT of the programmer. The intent is to test if the flag is TRUE or FALSE.
SAS only has two type of variables. Fixed length character strings and floating point numbers.
SAS will treat a numeric value of 0 or missing as FALSE and any other value as TRUE when performing a boolean test, such as in your first example code. SAS will generate 0 for true and 1 for FALSE as the result of any boolean operator. So boolean flags created by SAS such as FIRST. and LAST. will be either 0 or 1. You can treat them as boolean values (if first.product) or treat them as numbers (if last.product=1).
@Tom wrote:
SAS will treat a numeric value of 0 or missing as FALSE and any other value as TRUE when performing a boolean test, such as in your first example code. SAS will generate 0 for true and 1 for FALSE as the result of any boolean operator. So boolean flags created by SAS such as FIRST. and LAST. will be either 0 or 1. You can treat them as boolean values (if first.product) or treat them as numbers (if last.product=1).
is this a typo? 0 for TRUE 1 for FALSE?
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.