{"id":2394,"date":"2022-06-15T15:19:45","date_gmt":"2022-06-15T14:19:45","guid":{"rendered":"https:\/\/www.microdata.no\/?post_type=eksempel&#038;p=2394"},"modified":"2023-08-18T13:31:06","modified_gmt":"2023-08-18T12:31:06","slug":"restructure-datasets-from-wide-to-long-format","status":"publish","type":"eksempel","link":"https:\/\/www.microdata.no\/en\/eksempel\/restructure-datasets-from-wide-to-long-format\/","title":{"rendered":"Restructure datasets from wide to long format"},"content":{"rendered":"\n<p>For statistics and analyses in microdata.no, datasets created through the command&nbsp;<code>import<\/code>&nbsp;are normally used. These are data sets of the \u201cwide\u201d type, where information about all units in a population is structured horizontally at a variable level. The new&nbsp;<code>reshape-to-panel<\/code>&nbsp;command now makes it possible to change the data structure to long-format (panel-format), where information about each unit (individual) is structured vertically at the observation \/ record level.<\/p>\n\n\n\n<p>Variables that are measured over several times and that you want in long \/ panel format, must be named through&nbsp;<code>reshape-to-panel<\/code>&nbsp;with specified prefixes that consist of the letters (prefix) from the original variable in the wide dataset. Other variables for which no prefix is \u200b\u200bspecified, typically information that is only measured once (gender, country of birth, etc), are automatically defined as fixed information and the values \u200b\u200bfor these are repeated for all sub-levels of each unit.<\/p>\n\n\n\n<p>The script below shows how to use <code>reshape-to-panel<\/code> in practice. First, a standard dataset of the wide type is created, consisting of a 1% random sample of everyone who was registered resident in Norway as of 1\/1 2018. In addition, the variables regstat (register status), civstat (civil status) and wage (annual salary) are imported for the years 2018-2020, as well as the fixed information gender. Then the command <code>reshape-to-panel<\/code> is used to restructure the dataset into long format (panel data). The data we want in long\/panel format is entered by specifying the prefix to the sets of variables (the letter part before numbers that refers to years etc.), in this case regstat, civstat and wage. Gender is a fixed information, and we therefore do not need to include a variable prefix for this in the command expression. The finished long data set will include the variable date@panel which contains the sublevel value of each unit. In this case, all units will have sublevels 18, 19 and 20 (i.e. three sublevels\/observations\/records each).<\/p>\n\n\n\n<p><\/p>\n\n\n<div id=\"rose-block_bfac5509f5735f4e4543951ecd186989\" class=\"rose-code codeblock-wrapper\">\n<pre tabindex=\"0\" class=\"codeblock\"><code>require no.ssb.fdb:23 as db\r\n\r\n\/\/First create a regular wide dataset consisting of a 1% sample of all residents per 1\/1 2018\r\ncreate-dataset wide\r\nimport db\/BEFOLKNING_STATUSKODE 2018-01-01 as regstat18\r\nkeep if regstat18 == '1'\r\nsample 0.01 333\r\nimport db\/BEFOLKNING_STATUSKODE 2019-01-01 as regstat19\r\nimport db\/BEFOLKNING_STATUSKODE 2020-01-01 as regstat20\r\nimport db\/SIVSTANDFDT_SIVSTAND 2018-01-01 as civstat18\r\nimport db\/SIVSTANDFDT_SIVSTAND 2019-01-01 as civstat19\r\nimport db\/SIVSTANDFDT_SIVSTAND 2020-01-01 as civstat20\r\nimport db\/BEFOLKNING_KJOENN as gender\r\nimport db\/INNTEKT_WLONN 2018-01-01 as wage18\r\nimport db\/INNTEKT_WLONN 2019-01-01 as wage19\r\nimport db\/INNTEKT_WLONN 2020-01-01 as wage20\r\n\r\n\/\/Generate statistics\r\ntabulate regstat18, missing\r\ntabulate regstat19, missing\r\ntabulate regstat20, missing\r\ntabulate civstat18, missing\r\ntabulate civstat19, missing\r\ntabulate civstat20, missing\r\ntabulate gender, missing\r\n\r\nsummarize wage18 wage19 wage20\r\n\r\n\/\/Convert to long format (paneldata)\r\nreshape-to-panel regstat civstat wage\r\n\r\n\/\/Test for compliance with the count for the wide dataset\r\ntabulate date@panel, missing\r\n\r\ntabulate-panel regstat, missing\r\ntabulate-panel civstat, missing\r\ntabulate-panel regstat civstat, missing\r\n\r\ntabulate-panel gender, missing\r\ntabulate-panel regstat gender, missing\r\ntabulate-panel civstat gender, missing\r\n\r\nsummarize wage\r\nsummarize-panel wage<\/code><\/pre>\n<\/div>","protected":false},"parent":0,"menu_order":113,"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-2394","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>Restructure datasets from wide to long format - 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\/restrukturere-datasett-fra-wide-til-long-format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Restructure datasets from wide to long format - microdata.no\" \/>\n<meta property=\"og:description\" content=\"For statistics and analyses in microdata.no, datasets created through the command&nbsp;import&nbsp;are normally used. These are data sets of the \u201cwide\u201d type, where information about all units in a population is structured horizontally at a variable level. The new&nbsp;reshape-to-panel&nbsp;command now makes it possible to change the data structure to long-format (panel-format), where information about each unit...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/\" \/>\n<meta property=\"og:site_name\" content=\"microdata.no\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-18T12:31:06+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\\\/restrukturere-datasett-fra-wide-til-long-format\\\/\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-wide-til-long-format\\\/\",\"name\":\"Restructure datasets from wide to long format - microdata.no\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\"},\"datePublished\":\"2022-06-15T14:19:45+00:00\",\"dateModified\":\"2023-08-18T12:31:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-wide-til-long-format\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-wide-til-long-format\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-wide-til-long-format\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Restructure datasets from wide to long format\"}]},{\"@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":"Restructure datasets from wide to long format - 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\/restrukturere-datasett-fra-wide-til-long-format\/","og_locale":"en_US","og_type":"article","og_title":"Restructure datasets from wide to long format - microdata.no","og_description":"For statistics and analyses in microdata.no, datasets created through the command&nbsp;import&nbsp;are normally used. These are data sets of the \u201cwide\u201d type, where information about all units in a population is structured horizontally at a variable level. The new&nbsp;reshape-to-panel&nbsp;command now makes it possible to change the data structure to long-format (panel-format), where information about each unit...","og_url":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/","og_site_name":"microdata.no","article_modified_time":"2023-08-18T12:31:06+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\/restrukturere-datasett-fra-wide-til-long-format\/","url":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/","name":"Restructure datasets from wide to long format - microdata.no","isPartOf":{"@id":"https:\/\/www.microdata.no\/#website"},"datePublished":"2022-06-15T14:19:45+00:00","dateModified":"2023-08-18T12:31:06+00:00","breadcrumb":{"@id":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-wide-til-long-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/www.microdata.no\/en\/"},{"@type":"ListItem","position":2,"name":"Restructure datasets from wide to long format"}]},{"@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\/2394","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=2394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}