{"id":2994,"date":"2023-01-16T15:15:34","date_gmt":"2023-01-16T14:15:34","guid":{"rendered":"https:\/\/www.microdata.no\/?post_type=eksempel&#038;p=2994"},"modified":"2023-10-27T16:39:19","modified_gmt":"2023-10-27T15:39:19","slug":"different-ways-to-create-multicategory-variables","status":"publish","type":"eksempel","link":"https:\/\/www.microdata.no\/en\/eksempel\/different-ways-to-create-multicategory-variables\/","title":{"rendered":"Different ways to create multicategory variables"},"content":{"rendered":"\n<p>Coding of multicategory variables can be done in many different ways. The simplest solution is to use the commands <code>generate<\/code> and <code>replace<\/code> to code one category at a time. This works fine for a few categories. You then start by coding an output value using <code>generate<\/code>, and then use <code>replace<\/code> commands to moderate values \u200b\u200bbased on conditions (one command line for each value). The disadvantage of this is that you risk ending up with many command lines and long scripts that require resources and take a long time to run.<\/p>\n\n\n\n<p>If you want to code many categories, possibly use complicated conditions, then it is recommended to use the <code>recode()<\/code> command. This can be used to set up all the code conditions in a single command statement, making scripts more compact and faster to run. Through <code>recode()<\/code> you can, among other things, enter value intervals and create associated value labels (so that you don&#8217;t have to do this afterwards through the <code>define-labels<\/code> and <code>assign-labels<\/code> commands).<\/p>\n\n\n\n<p>A third set of tools for setting up code expressions for multicategory variables are the <code>inlist()<\/code> and <code>inrange()<\/code> functions. These are ideal if you want to create extensive code conditions, usually in combination with <code>generate<\/code> and <code>replace<\/code>, 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<p>For those who want to set up code expressions for many categories, there is a fourth option: Automatic generation of recoding by uploading a punctuation-separated recoding file. <a href=\"https:\/\/microdata.no\/manual\/en\/brukermanual\/Tilrettelegging%20av%20variabler\/3.2.1%20Automatisk%20omkoding%20ved%20hjelp%20av%20opplasting%20av%20skilletegnseparerte%20filer\">For more on this, click here.<\/a><\/p>\n\n\n\n<p>You will find more information about using <code>generate<\/code>, <code>replace<\/code>, <code>recode()<\/code>, <code>inlist()<\/code>, <code>inrange()<\/code> and automatic recoding in the User Guide chapters 3.1 &#8211; 3.2.<\/p>\n\n\n\n<p>This script demonstrates the different ways to code multicategory variables:<\/p>\n\n\n<div id=\"rose-block_91637e789f3b01073469edae6fc47f27\" class=\"rose-code codeblock-wrapper\">\n<pre tabindex=\"0\" class=\"codeblock\"><code>require no.ssb.fdb:23 as ds\r\n\r\ncreate-dataset demo\r\n\r\n\/\/Lage flere kategorier vha generate og replace\r\nimport ds\/INNTEKT_BRUTTOFORM 2020-01-01 as formue\r\n\r\ngenerate formueint = 1\r\nreplace formueint = 2 if formue > 500000\r\nreplace formueint = 3 if formue > 1000000\r\nreplace formueint = 4 if formue > 1500000\r\n\r\ntabulate formueint\r\n\r\n\r\n\/\/Lage flere kategorier (verdensregioner) vha recode\r\ncreate-dataset befolkning\r\nimport ds\/BEFOLKNING_STATUSKODE 2021-01-01 as statuskode\r\nkeep if statuskode == '1'\r\n\r\nimport ds\/BEFOLKNING_FODELAND as f\u00f8deland\r\ntabulate f\u00f8deland\r\n\r\ndestring f\u00f8deland\r\nrecode f\u00f8deland (111 120 138 139 140 148 155 156 159\/164 = 2 'Europeiske land utenom EU') (101\/141 144\/158 = 1 'EU\/E\u00d8S') (203\/393 = 3 'Afrika') (143 404\/578 = 4 'Asia med Tyrkia') (612 684 = 5 'Nord-Amerika') (601\/775 = 6 'S\u00f8r- og Mellom-Amerika') (802\/840 = 7 'Oseania') (980 = 8 'Statsl\u00f8se') (990 = 9 'Uoppgitt')\r\ntabulate f\u00f8deland\r\n\r\n\r\n\/\/Lager kode for storby basert p\u00e5 kommunenumre til de fire st\u00f8rste kommunene ved bruk av inlist()\r\nimport ds\/BOSATTEFDT_BOSTED 2021-12-31 as kommune\r\ngenerate storby = 0\r\nreplace storby = 1 if inlist(kommune,'0301','4601','1103','5001')\r\ntabulate kommune if storby, rowsort()\r\n\r\n\r\n\/\/Grupperer \u00e5rlig l\u00f8nnsinntekt i seks grupper ved bruk av inrange()\r\nimport ds\/INNTEKT_LONN 2021-12-31 as l\u00f8nn\r\n\r\ngenerate l\u00f8nn_gr = 0\r\nreplace l\u00f8nn_gr = 1 if inrange(l\u00f8nn,1,200000)\r\nreplace l\u00f8nn_gr = 2 if inrange(l\u00f8nn,200001,400000)\r\nreplace l\u00f8nn_gr = 3 if inrange(l\u00f8nn,400001,600000)\r\nreplace l\u00f8nn_gr = 4 if inrange(l\u00f8nn,600001,800000)\r\nreplace l\u00f8nn_gr = 5 if l\u00f8nn > 800000\r\n\r\ndefine-labels l\u00f8nn_int 0 '0 kr' 1 '1 - 200 000 kr' 2 '200001 - 400 000 kr' 3 '400 001 - 600 000 kr' 4 '600 001 - 800 000 kr' 5 '800 000 kr ->'\r\nassign-labels l\u00f8nn_gr l\u00f8nn_int\r\ntabulate l\u00f8nn_gr\r\n\r\n\/\/Alternativ m\u00e5te \u00e5 gruppere l\u00f8nnsinntekt ved bruk av recode\r\nreplace l\u00f8nn = 0 if sysmiss(l\u00f8nn)\r\nrecode l\u00f8nn (1\/200000 = 1)(200001\/400000 = 2)(400001\/600000 = 3)(600001\/800000 = 4)(800001\/max = 5)\r\nassign-labels l\u00f8nn l\u00f8nn_int\r\ntabulate l\u00f8nn<\/code><\/pre>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"parent":0,"menu_order":16,"template":"","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":""},"class_list":["post-2994","eksempel","type-eksempel","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Different ways to create multicategory variables - 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\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Different ways to create multicategory variables - microdata.no\" \/>\n<meta property=\"og:description\" content=\"Coding of multicategory variables can be done in many different ways. The simplest solution is to use the commands generate and replace to code one category at a time. This works fine for a few categories. You then start by coding an output value using generate, and then use replace commands to moderate values \u200b\u200bbased...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/\" \/>\n<meta property=\"og:site_name\" content=\"microdata.no\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-27T15:39:19+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/ulike-mater-a-lage-flerkategorivariabler\\\/\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/ulike-mater-a-lage-flerkategorivariabler\\\/\",\"name\":\"Different ways to create multicategory variables - microdata.no\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\"},\"datePublished\":\"2023-01-16T14:15:34+00:00\",\"dateModified\":\"2023-10-27T15:39:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/ulike-mater-a-lage-flerkategorivariabler\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/ulike-mater-a-lage-flerkategorivariabler\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/ulike-mater-a-lage-flerkategorivariabler\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Different ways to create multicategory variables\"}]},{\"@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\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Different ways to create multicategory variables - 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\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/","og_locale":"en_US","og_type":"article","og_title":"Different ways to create multicategory variables - microdata.no","og_description":"Coding of multicategory variables can be done in many different ways. The simplest solution is to use the commands generate and replace to code one category at a time. This works fine for a few categories. You then start by coding an output value using generate, and then use replace commands to moderate values \u200b\u200bbased...","og_url":"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/","og_site_name":"microdata.no","article_modified_time":"2023-10-27T15:39:19+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/","url":"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/","name":"Different ways to create multicategory variables - microdata.no","isPartOf":{"@id":"https:\/\/www.microdata.no\/#website"},"datePublished":"2023-01-16T14:15:34+00:00","dateModified":"2023-10-27T15:39:19+00:00","breadcrumb":{"@id":"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.microdata.no\/eksempel\/ulike-mater-a-lage-flerkategorivariabler\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/www.microdata.no\/en\/"},{"@type":"ListItem","position":2,"name":"Different ways to create multicategory variables"}]},{"@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"}]}},"taxonomy_info":[],"featured_image_src_large":[],"author_info":[],"comment_info":"","_links":{"self":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/eksempel\/2994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/eksempel"}],"about":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/types\/eksempel"}],"wp:attachment":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/media?parent=2994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}