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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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.

Buzzy_Bee
Quartz | Level 8
Thanks for listing those two examples. The use of the ordinal function is a particularly nice solution.
Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 686 views
  • 5 likes
  • 3 in conversation