conditonal processing when too many conditions,how?

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

conditonal processing when too many conditions,how?

Hey folks,

I m wondering how to write conditional processing statements  when there are too many conditions be it simple or medium complex. If then else doesn't seem to work, select when also seems long writing or may be its that i don't quite know how to smartly write by con-sizing it.  I'm sure you folks agree that it can be boring too. Is there a way out?

1. is there a golden rule or standards to go only for select when style when its too many?

2. if there is any way out, can you guys please quote or explain with examples- I did research a fair bit online, but didnt get any clear examples that explains with too many conditions.

Thanks for your time,

Cheers,

Andy


Accepted Solutions
Solution
‎05-07-2013 08:06 AM
Super Contributor
Posts: 644

Re: conditonal processing when too many conditions,how?

I favour formats when applicable, and merges when the joins are not too complex.  But there are other techniques to keep in mind which can also be useful:

The    select  ... when   ...   otherwise ... end   structure is very flexible and more easy to read than a series of concatenated    if  ...  then   ... else if ... then ...   statements. 

Used in conjunction with   link  ... return  you can convert segments of conditional programming into subroutines within a data step.  Or the subroutines could be held in individual macros which are invoked by the when statements:

     Select (continent) ;

          When ('Africa') do %Africa_processing; end ;

          ...

          Otherwise do %Rest_of_the world; end ;

     End ;

Richard

View solution in original post


All Replies
Super User
Posts: 17,865

Re: conditonal processing when too many conditions,how?

you need to provide more details on what your conditional processing is.

depending on the needs the solution could be a macro, arrays or a format just to start off, but the answer depends on the question.

Contributor
Posts: 56

Re: conditonal processing when too many conditions,how?

Hmm , that's interesting you mentioned macro and arrays. OK, basically i deal with a lot of data that contains demographic information. Therefore the information gets very granular as its get explored. for example, by location, by country, by region , by state, by village, other dimensions such and beyond. Having mentioned the list, the condition to be applied is various within each parameter. For example, if you take country, If country would mean like  60 different countries, and therefore it goes far and wide within to within or within and out totally unrelated.

If country = list them all? and  then give condition

if village= list all villages and then pass condition, here there could be 1000's of villages:smileyconfused:

so, thats the kind I am working on. I'm not sure if i explained well enough, I'll see if i can simulate an unofficial look alike data of business requirement. Please accept my apologies. thanks.

Andy

Super Contributor
Posts: 394

Re: conditonal processing when too many conditions,how?

Maybe the techniques described in this SGF paper by Art Carpenter will help. I'm thinking of the section entitled "The Classic Table Lookup".

Super User
Posts: 5,085

Re: conditonal processing when too many conditions,how?

Andy,

I'm fairly certain that formats will be at least part of the solution.  If you had a DATA step that included something like this:

if village in ("Village 1", "Village 2", "Village 3", ... "Village 50") then village_group='A';

The alternative would be to create a format:

proc format;

value $village "Village 1", "Village 2", "Village 3", ..., "Village 50" = 'A'

                      other=' ';

run;

The workload is similar, but now the DATA step becomes much easier to follow.  To get the equivalent result, a DATA step would code:

village_group = put(village, $village.);

Moreover, you can permanently save the format and use it in many programs.  If the format were to change, all the programs that use it would be updated automatically.  You'll need to look at how to permanently save formats, how to retrieve permanently saved formats, and perhaps how to use the CNTLIN= option to create a format using a SAS data set as input instead of having to type out the complete VALUE statement.

Good luck.

Contributor
Posts: 56

Re: conditonal processing when too many conditions,how?

Hi,

Very nice idea and thought. That's pretty much the kind I was looking for, however for whatever reason the use of proc format in this instance didn't strike my mind. The point is i. efficiency ii. easy to use and reuse. Like you mentioned, even if I could make part of the solution smaller and efficient rather than like writing an English essay would mean so much for the process. Thanks so much.Have a good one.

Cheers mate,

Andy

Solution
‎05-07-2013 08:06 AM
Super Contributor
Posts: 644

Re: conditonal processing when too many conditions,how?

I favour formats when applicable, and merges when the joins are not too complex.  But there are other techniques to keep in mind which can also be useful:

The    select  ... when   ...   otherwise ... end   structure is very flexible and more easy to read than a series of concatenated    if  ...  then   ... else if ... then ...   statements. 

Used in conjunction with   link  ... return  you can convert segments of conditional programming into subroutines within a data step.  Or the subroutines could be held in individual macros which are invoked by the when statements:

     Select (continent) ;

          When ('Africa') do %Africa_processing; end ;

          ...

          Otherwise do %Rest_of_the world; end ;

     End ;

Richard

Contributor
Posts: 56

Re: conditonal processing when too many conditions,how?

Awesome thought Richard!! makes great sense...Thank you

Super Contributor
Posts: 418

Re: conditonal processing when too many conditions,how?

A similar way to do what Astounding said above is too just save format data tables with the values that you want associated with each. Ex:

Table

Village               Variable to show

Village 1                    A

Village 2                    B

Village 3                    C

etc..

And then you simply merge onto this table by village, and your new column will show the mappings that you want. If you are doing multiple if, then statements (if and, if or.. etc..) then this obviously will not work, however you could also macroize your process, or convert some of it into array work (I'm guessing).

Your question is a little too vague, (including your detailed answer response) to fully understand what you need exactly.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 342 views
  • 7 likes
  • 6 in conversation