{"id":3000,"date":"2023-01-17T13:27:12","date_gmt":"2023-01-17T12:27:12","guid":{"rendered":"https:\/\/www.microdata.no\/?p=3000"},"modified":"2023-11-22T14:36:05","modified_gmt":"2023-11-22T13:36:05","slug":"more-compressed-recoding-of-variables-inlist-and-inrange","status":"publish","type":"post","link":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/","title":{"rendered":"More compressed recoding of variables: inlist and inrange"},"content":{"rendered":"\n<p><strong><em>The functions <code>inlist<\/code> and <code>inrange<\/code> can be used to set up compressed code expressions for coding variables, so that scripts are more clear and less resource-demanding to run.<\/em><\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>These functions are ideal if you want to create extensive code conditions, usually in combination with <code>generate<\/code> and <code>replace<\/code> expressions, e.g. if you want to make a rough grouping of municipalities, where you need to list larger sets of municipality codes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The inlist function<\/h2>\n\n\n\n<p>When coding\/recoding variables based on long if conditions, the function <code>inlist<\/code> can be useful to use. An example is if you want to create a variable that takes the value 1 for people who live in a sample of 100 municipalities. This would have given a <code>generate<\/code> or <code>replace<\/code> command with a very long if expression. When using <code>inlist<\/code>, this can be made more compressed by listing all the municipality numbers separated by commas inside function brackets.<\/p>\n\n\n\n<p>The logical function <code>inlist<\/code> is set to 1 (&#8220;true&#8221;) if the value in the first argument is found among the remaining arguments.<\/p>\n\n\n\n<p>The arguments to the function can be both variables and values \u200b\u200b(all types).<\/p>\n\n\n\n<p>Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>generate var = 1 if inlist(marital_state, 1, 3, 5)<\/code><br><em>(inlist function = 1 (\u201ctrue\u201d) if marital_state = 1, 3 or 5)<\/em><\/li>\n\n\n\n<li><code>generate var = 1 if inlist('1', regstat09, regstat10, regstat11)<\/code><br><em>(inlist function = 1 (\u201ctrue\u201d) if at least one of the regstat variables = &#8216;1&#8217;)<\/em><\/li>\n<\/ul>\n\n\n\n<p>The examples above correspond to these expressions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>generate var = 1 if marital_state == 1 | marital_state == 3 | marital_state == 5<\/code><\/li>\n\n\n\n<li><code>generate var = 1 if regstat09 == '1' | regstat10 == '1' | regstat11 == '1'<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The inrange function<\/h2>\n\n\n\n<p>In addition to <code>inlist<\/code>, there is another logical function that can make it easier to create conditions based on intervals: <code>inrange<\/code>. If conditions based on intervals rarely become particularly long, since you usually only need to specify a minimum and a maximum value. But if you operate with several intervals in one and the same if-expression, inrange can be useful. In any case, the code syntax becomes more compressed compared to regular if statements when using this function.<\/p>\n\n\n\n<p>The logical function <code>inrange<\/code> is set to 1 (&#8220;true&#8221;) if the value in the first argument is higher than or equal to the value in the second argument and lower than or equal to the value in the third argument.<\/p>\n\n\n\n<p>The arguments to the function can be both variables and values \u200b\u200b(all types).<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>generate var = 1 if inrange(wealth, 500000, 1000000)<\/code><br><em>(inrange function = 1 (\u201ctrue\u201d) if 500000 &lt;= wealth &lt;= 1000000)<\/em><\/li>\n<\/ul>\n\n\n\n<p>The example above corresponds to this expression:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>generate var = 1 if wealth &gt;= 500000 &amp; wealth &lt;= 1000000<\/code><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/microdata.no\/manual\/en\/eksempel\/Grunnleggende%20operasjoner\/Ulike%20m%C3%A5ter%20%C3%A5%20lage%20flerkategorivariabler\">Click here for a practical example of using inlist and inrange.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The functions inlist and inrange can be used to set up compressed code expressions for coding variables, so that scripts are more clear and less resource-demanding to run.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-3000","post","type-post","status-publish","format-standard","hentry","category-news"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>More compressed recoding of variables: inlist and inrange - microdata.no<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"More compressed recoding of variables: inlist and inrange - microdata.no\" \/>\n<meta property=\"og:description\" content=\"The functions inlist and inrange can be used to set up compressed code expressions for coding variables, so that scripts are more clear and less resource-demanding to run.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/\" \/>\n<meta property=\"og:site_name\" content=\"microdata.no\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-17T12:27:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-22T13:36:05+00:00\" \/>\n<meta name=\"author\" content=\"Trond Pedersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trond Pedersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/\"},\"author\":{\"name\":\"Trond Pedersen\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#\\\/schema\\\/person\\\/76761ddfe0d06e3f08f5491a9faeab92\"},\"headline\":\"More compressed recoding of variables: inlist and inrange\",\"datePublished\":\"2023-01-17T12:27:12+00:00\",\"dateModified\":\"2023-11-22T13:36:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/\"},\"wordCount\":364,\"articleSection\":[\"News\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/\",\"name\":\"More compressed recoding of variables: inlist and inrange - microdata.no\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\"},\"datePublished\":\"2023-01-17T12:27:12+00:00\",\"dateModified\":\"2023-11-22T13:36:05+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#\\\/schema\\\/person\\\/76761ddfe0d06e3f08f5491a9faeab92\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/more-compressed-recoding-of-variables-inlist-and-inrange\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"More compressed recoding of variables: inlist and inrange\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/\",\"name\":\"microdata.no\",\"description\":\"Gj\u00f8r det enklere \u00e5 analysere registerdata\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.microdata.no\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#\\\/schema\\\/person\\\/76761ddfe0d06e3f08f5491a9faeab92\",\"name\":\"Trond Pedersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g\",\"caption\":\"Trond Pedersen\"},\"url\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/author\\\/trond\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"More compressed recoding of variables: inlist and inrange - microdata.no","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/","og_locale":"en_US","og_type":"article","og_title":"More compressed recoding of variables: inlist and inrange - microdata.no","og_description":"The functions inlist and inrange can be used to set up compressed code expressions for coding variables, so that scripts are more clear and less resource-demanding to run.","og_url":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/","og_site_name":"microdata.no","article_published_time":"2023-01-17T12:27:12+00:00","article_modified_time":"2023-11-22T13:36:05+00:00","author":"Trond Pedersen","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Trond Pedersen","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/#article","isPartOf":{"@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/"},"author":{"name":"Trond Pedersen","@id":"https:\/\/www.microdata.no\/#\/schema\/person\/76761ddfe0d06e3f08f5491a9faeab92"},"headline":"More compressed recoding of variables: inlist and inrange","datePublished":"2023-01-17T12:27:12+00:00","dateModified":"2023-11-22T13:36:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/"},"wordCount":364,"articleSection":["News"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/","url":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/","name":"More compressed recoding of variables: inlist and inrange - microdata.no","isPartOf":{"@id":"https:\/\/www.microdata.no\/#website"},"datePublished":"2023-01-17T12:27:12+00:00","dateModified":"2023-11-22T13:36:05+00:00","author":{"@id":"https:\/\/www.microdata.no\/#\/schema\/person\/76761ddfe0d06e3f08f5491a9faeab92"},"breadcrumb":{"@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.microdata.no\/en\/more-compressed-recoding-of-variables-inlist-and-inrange\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/www.microdata.no\/en\/"},{"@type":"ListItem","position":2,"name":"More compressed recoding of variables: inlist and inrange"}]},{"@type":"WebSite","@id":"https:\/\/www.microdata.no\/#website","url":"https:\/\/www.microdata.no\/","name":"microdata.no","description":"Gj\u00f8r det enklere \u00e5 analysere registerdata","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.microdata.no\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.microdata.no\/#\/schema\/person\/76761ddfe0d06e3f08f5491a9faeab92","name":"Trond Pedersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b90e3f42c839e825d86949fc2f9a318f2a81da5f9e6b1431ff4d872333d4e717?s=96&d=mm&r=g","caption":"Trond Pedersen"},"url":"https:\/\/www.microdata.no\/en\/author\/trond\/"}]}},"taxonomy_info":{"category":[{"value":2,"label":"News"}]},"featured_image_src_large":false,"author_info":{"display_name":"Trond Pedersen","author_link":"https:\/\/www.microdata.no\/en\/author\/trond\/"},"comment_info":0,"category_info":[{"term_id":2,"name":"News","slug":"news","term_group":0,"term_taxonomy_id":2,"taxonomy":"category","description":"","parent":0,"count":79,"filter":"raw","cat_ID":2,"category_count":79,"category_description":"","cat_name":"News","category_nicename":"news","category_parent":0}],"tag_info":false,"_links":{"self":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/posts\/3000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/comments?post=3000"}],"version-history":[{"count":3,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/posts\/3000\/revisions"}],"predecessor-version":[{"id":3827,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/posts\/3000\/revisions\/3827"}],"wp:attachment":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/media?parent=3000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/categories?post=3000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/tags?post=3000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}