Hello, I need to create a variable from a large set of other variables (>100) using an IF THEN statement. I know how to do this the "hard" way in my case by listing each single variable separated by an "or" condition:
IF var='a' or var='b' or var='c' or var='d' or var='e'...... THEN new_var=1; ELSE new_var=0;
However, considering the large number of variables I am dealing with, I am wondering if there is a more efficient way of creating such a variable (such as just copying/pasting all the variables and using on only one "var=, or" condition) rather than listing every single variable after "var=" and followed by my "or" condition. Please note that the 'var' variable is a character variable and again, there are >100 different values of "var' that I need to include. I also need the condition to be an "or" condition, not "and."
Any help with this would be much appreciated. Thank you very much.
Your post invites this question. Do you really have 100 variables, or do you have one variable that might take on 100 different values?
If it's the latter, here's an abbreviation for checking them:
if var in ('a', 'b', 'c', 'd', 'e') then new_var=1;
There are other ways, but let's first clarify the real problem here.
Your post invites this question. Do you really have 100 variables, or do you have one variable that might take on 100 different values?
If it's the latter, here's an abbreviation for checking them:
if var in ('a', 'b', 'c', 'd', 'e') then new_var=1;
There are other ways, but let's first clarify the real problem here.
Yes, I apologize for misspeaking. I need to include many different values of one variable (e.g., var), not 100 different variables.
I tried your code and it worked. Thanks!
Out of curiosity, what are other ways of creating this variable given this situation? What if I needed to have an "and" condition instead of "or"?
If you only have one variable, "and" is logically impossible. This comparison can never be true:
if var="a" and var="b" then new_var=1;
There are other (usually more complex) ways to solve the "or" problem. But for any solution to work, you can't escape having to type out all the values once.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.