a lista as condition

17 views
Skip to first unread message

Ingrid Schockaert

unread,
Aug 21, 2018, 10:03:47 AM8/21/18
to liam2-users
dear all,

I want to do something like this:


agegroup: if (municipality_id is in "list of municipality_ids" ,
             5 * trunc(age / 5),
             age)


In other words, only if the municipality_id of an individual is part of a list of predetermined municipalities, than create a new age variable.

Is that possible in Liam2.

Regards,
Ingrid

Djibril Togola

unread,
Aug 21, 2018, 11:17:18 AM8/21/18
to liam2...@googlegroups.com
Dear Ingrid:

I think you have at least one solution to the issue you've exposed :

*** If municipality_id has only one modality, a simple egality test should solve the problem like this :
agegroup: if ( "list of municipality_ids" = = municipality_id ,
             5 * trunc(age / 5),
             age)

*** If municipality_id has more than one modality, you still can use a simple egality test, either by creating an intermediate boolean variable either by creating both variables   ("list of municipality_ids"  and municipality_id) as some fields of the entity individuals.

Good luck, 

------
Djibril TOGOLA



Djibril...@u-pec.fr / gmail.com


--
You received this message because you are subscribed to the Google Groups "liam2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liam2-users...@googlegroups.com.
To post to this group, send email to liam2...@googlegroups.com.
Visit this group at https://groups.google.com/group/liam2-users.

Gaëtan de Menten

unread,
Aug 24, 2018, 6:13:55 AM8/24/18
to liam2...@googlegroups.com

Hello Ingrid,

 

Sadly, LIAM2 does not support this operation natively (yet). The good news is that you can emulate it relatively easily.

 

If your list of values (municipality_ids) is short, the simplest is to use an "or" condition:

 

if(municipality_id == 10 or municipality_id == 13 or municipality_id == 17,

   5 * trunc(age / 5),
   age)

 

If it is longer than a few elements or if the list is determined dynamically, using a loop will be more practical:

 

num_minicipalities: 30

in_mun_list: False                 

poslist: 0

while poslist < num_municipalities:

    in_mun_list: in_mun_list or municipality_id == municipality_ids[poslist]

   poslist: poslist + 1

res: if(in_mun_list,

        5 * trunc(age / 5),
        age)

 

But it would probably be cleaner to extract a reusable function out of this:

 

    inlist(expr, valuelist):

        - invaluelist: False

        - poslist: 0

        - while poslist < valuelist.__len__() and not all(invaluelist):

            - invaluelist: invaluelist or expr == valuelist[poslist]

            - poslist: poslist + 1

        - return invaluelist

 

    other_func():

        - municipality_ids: [10, 20, 30]

       - in_mun_list: inlist(municipality_id, municipality_ids)

  - res: if(in_mun_list,

            5 * trunc(age / 5),
            age)

        # or even, do everything in a single expression:

        - res: if(inlist(municipality_id, [10, 20, 30]),

            5 * trunc(age / 5),
            age)

 

 

Hope it helps,

Gaëtan

 

--

You received this message because you are subscribed to the Google Groups "liam2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liam2-users...@googlegroups.com.
To post to this group, send email to liam2...@googlegroups.com.
Visit this group at https://groups.google.com/group/liam2-users.



Disclaimer: This e-mail may contain confidential information which is intended only for the use of the recipient(s) named above.
If you have received this communication in error, please notify the sender immediately and delete this e-mail from your system.
Please note that e-mail messages cannot be considered as official information from the Federal Planning Bureau.

Ingrid Schockaert

unread,
Aug 30, 2018, 5:46:42 AM8/30/18
to liam2-users
Thanks to all for the reactions.

Its very clear now...it was the expression "inlist" I was looking for...

merci!
Reply all
Reply to author
Forward
0 new messages