I was reading some code today at work that someone else had written, and it looks something like this code below (I've used the SASHELP.BASEBALL table as example instead of my work data set). They've got several variables that start with the same characters and then various and/or conditions. This example is simplified, but in my work example it contains about 15 if/else statements and I wanted to make the code more concise.
I know that the colon can be used to easily drop several variables starting with the same characters (for example, I can use drop n: to drop all the variables in the Baseball data set that start with n, and the set statement also allows users to set several tables starting with the same letters and using colon). I would have thought SAS would allow me to use the same colon method on the variables after the if, for example, if cr: >=200 then group='High', but it doesn't allow it. I know you can use cr: in an array. Is that the best way to tidy up many columns that start the same characters, or should I be trying something else? Thanks for your ideas.
data want;
set SASHELP.BASEBALL;
if crHits >= 200 and crHome >= 200 and crRuns >= 200 or crRbi>=200 then group='High';
else if crHits >= 100 or (crHome >= 100 and crRuns >= 100) then group='Med';
drop n: ;
run;
Hi @Buzzy_Bee,
I think the interpretation of a hypothetical condition like "cr: >=200" would not be clear. Would it mean for the variables with names starting with "cr" that all of them are >=200 or at least one of them?
Note that neither of these two interpretations would be applicable to your example IF conditions: Your AND/OR combinations are logically different from both interpretations. Moreover, the variable list cr: would include existing variables such as CrAtBat or CrBB which are not considered in your conditions.
In addition to arrays you can use variable lists (like cr:, a1-a10 or name--weight) in SAS functions. For example, if, like in SASHELP.BASEBALL, no missing values are present in the variables involved, you could abbreviate the first IF condition to
max(min(of crHits--crRuns),crRbi) >= 200
In the (potential) presence of missing values
max(ordinal(1, of crHits--crRuns),crRbi) >= 200
would ensure that missings among crHits, crHome and crRuns are not ignored when the minimum is determined.
Hi @Buzzy_Bee,
I think the interpretation of a hypothetical condition like "cr: >=200" would not be clear. Would it mean for the variables with names starting with "cr" that all of them are >=200 or at least one of them?
Note that neither of these two interpretations would be applicable to your example IF conditions: Your AND/OR combinations are logically different from both interpretations. Moreover, the variable list cr: would include existing variables such as CrAtBat or CrBB which are not considered in your conditions.
In addition to arrays you can use variable lists (like cr:, a1-a10 or name--weight) in SAS functions. For example, if, like in SASHELP.BASEBALL, no missing values are present in the variables involved, you could abbreviate the first IF condition to
max(min(of crHits--crRuns),crRbi) >= 200
In the (potential) presence of missing values
max(ordinal(1, of crHits--crRuns),crRbi) >= 200
would ensure that missings among crHits, crHome and crRuns are not ignored when the minimum is determined.
Using the wildcard creates a list of variables, which means you can use it in all places where a list of variables is syntactically correct.
A condition does not accept a list of variables, therefore you can't use the wildcard there.
if var1 var2 var3 = x
is quite obviously not correct and not translatable for the data step compiler.
Either use an array through which you can loop, or a function that accepts lists through the of: operator.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: