{"id":2574,"date":"2022-06-24T13:45:52","date_gmt":"2022-06-24T12:45:52","guid":{"rendered":"https:\/\/www.microdata.no\/?post_type=eksempel&#038;p=2574"},"modified":"2023-08-18T13:31:06","modified_gmt":"2023-08-18T12:31:06","slug":"restructure-datasets-from-long-to-wide-format","status":"publish","type":"eksempel","link":"https:\/\/www.microdata.no\/en\/eksempel\/restructure-datasets-from-long-to-wide-format\/","title":{"rendered":"Restructure datasets from long to wide format"},"content":{"rendered":"\n<p>The script below demonstrates how to use <code>reshape-from-panel<\/code>&nbsp;in practice.<\/p>\n\n\n\n<p>First, a regular data set of the wide type is created, consisting of a 1% random sample of everyone who was registered resident in Norway as of 1\/1 2019. The fully converted long data set will include the variable date@panel which contains the value at the sublevel of each single unit. In this case the values \u200b\u200b19 and 20. See separate analysis example for explanation of what happens when you use <code>reshape-to-panel<\/code>.<\/p>\n\n\n\n<p>Then <code>reshape-from-panel<\/code> is used to convert the data set to wide format again. This will then give us the same data set that we started with. Note that no variables or prefix are specified when using this command, and that all variables are converted to wide format with a suffix based on the associated sublevel. This also applies to gender, although this is fixed information. As in the example below, you can remove the redundant gender variables and rename them to &#8220;gender&#8221;.<\/p>\n\n\n\n<p>The last part of the example creates a classic panel dataset using the command <code>import-panel<\/code>, and then uses <code>reshape-from-panel<\/code> to convert the data to wide format. Note that here the suffixes are different. When using <code>tabulate-panel<\/code> or <code>summarize-panel<\/code> it will look like the sublevel has values \u200b\u200bof the type &#8220;YYYY-MM-DD&#8221;, but this only applies as a display format. The actual values \u200b\u200bfor date@panel in this case use reference dates as value format (number of days measured from 1\/1 1970). This can be solved by renaming the variable names with the command <code>rename<\/code>. Note that the converted dataset will be identical to the result of the conversion of the dataset created using <code>reshape-to-panel<\/code>. <\/p>\n\n\n<div id=\"rose-block_197002ff7dd704599e818bc7320816d6\" class=\"rose-code codeblock-wrapper\">\n<pre tabindex=\"0\" class=\"codeblock\"><code>require no.ssb.fdb:23 as db\r\n\r\n\/\/Create a wide dataset consisting of 1% of all residents per 1\/1 2019\r\ncreate-dataset wide\r\nimport db\/BEFOLKNING_STATUSKODE 2019-01-01 as regstat19\r\nkeep if regstat19 == '1'\r\nsample 0.01 333\r\nimport db\/BEFOLKNING_STATUSKODE 2020-01-01 as regstat20\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 2019-01-01 as wage19\r\nimport db\/INNTEKT_WLONN 2020-01-01 as wage20\r\n\r\ntabulate regstat19, missing\r\ntabulate regstat20, missing\r\ntabulate civstat19, missing\r\ntabulate civstat20, missing\r\ntabulate gender, missing\r\n\r\nsummarize wage19 wage20 \r\n\r\n\/\/Restructure to panel\/long format\r\nreshape-to-panel regstat civstat wage\r\n\r\ntabulate-panel regstat, missing\r\ntabulate-panel civstat, missing\r\ntabulate-panel gender, missing\r\nsummarize-panel wage\r\n\r\n\/\/Restructure back to wide format\r\nreshape-from-panel\r\ndrop gender20\r\nrename gender19 gender\r\n\r\ntabulate regstat19, missing\r\ntabulate regstat20, missing\r\ntabulate civstat19, missing\r\ntabulate civstat20, missing\r\ntabulate gender, missing\r\nsummarize wage19 wage20\r\n\r\n\r\n\/\/Create a new panel dataset for the same population through import-panel\r\nclone-units wide paneltest\r\nuse paneltest\r\nimport-panel db\/BEFOLKNING_STATUSKODE db\/SIVSTANDFDT_SIVSTAND db\/INNTEKT_WLONN db\/BEFOLKNING_KJOENN 2019-01-01 2020-01-01\r\nrename BEFOLKNING_STATUSKODE regstat\r\nrename SIVSTANDFDT_SIVSTAND civstat\r\nrename INNTEKT_WLONN wage\r\nrename BEFOLKNING_KJOENN gender\r\n\r\ntabulate-panel regstat, missing\r\ntabulate-panel civstat, missing\r\ntabulate-panel gender, missing\r\nsummarize-panel wage\r\n\r\n\/\/Restructure to wide format\r\nreshape-from-panel\r\ndrop gender18262\r\nrename gender17897 gender\r\n\r\nrename regstat17897 regstat19\r\nrename regstat18262 regstat20\r\n\r\nrename civstat17897 civstat19\r\nrename civstat18262 civstat20\r\n\r\nrename wage17897 wage19\r\nrename wage18262 wage20\r\n\r\nsummarize wage19 wage20<\/code><\/pre>\n<\/div>","protected":false},"parent":0,"menu_order":115,"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-2574","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 long to wide 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-long-til-wide-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 long to wide format - microdata.no\" \/>\n<meta property=\"og:description\" content=\"The script below demonstrates how to use reshape-from-panel&nbsp;in practice. First, a regular data set of the wide type is created, consisting of a 1% random sample of everyone who was registered resident in Norway as of 1\/1 2019. The fully converted long data set will include the variable date@panel which contains the value at the...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-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-long-til-wide-format\\\/\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-long-til-wide-format\\\/\",\"name\":\"Restructure datasets from long to wide format - microdata.no\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\"},\"datePublished\":\"2022-06-24T12:45:52+00:00\",\"dateModified\":\"2023-08-18T12:31:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-long-til-wide-format\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-long-til-wide-format\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/restrukturere-datasett-fra-long-til-wide-format\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Restructure datasets from long to wide 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 long to wide 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-long-til-wide-format\/","og_locale":"en_US","og_type":"article","og_title":"Restructure datasets from long to wide format - microdata.no","og_description":"The script below demonstrates how to use reshape-from-panel&nbsp;in practice. First, a regular data set of the wide type is created, consisting of a 1% random sample of everyone who was registered resident in Norway as of 1\/1 2019. The fully converted long data set will include the variable date@panel which contains the value at the...","og_url":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-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-long-til-wide-format\/","url":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-format\/","name":"Restructure datasets from long to wide format - microdata.no","isPartOf":{"@id":"https:\/\/www.microdata.no\/#website"},"datePublished":"2022-06-24T12:45:52+00:00","dateModified":"2023-08-18T12:31:06+00:00","breadcrumb":{"@id":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-format\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.microdata.no\/eksempel\/restrukturere-datasett-fra-long-til-wide-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/www.microdata.no\/en\/"},{"@type":"ListItem","position":2,"name":"Restructure datasets from long to wide 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\/2574","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=2574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}