{"id":301,"date":"2019-08-02T15:48:30","date_gmt":"2019-08-02T19:48:30","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=301"},"modified":"2020-09-16T09:31:52","modified_gmt":"2020-09-16T13:31:52","slug":"extracting-numbers-from-strings-in-excel","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2019\/08\/02\/extracting-numbers-from-strings-in-excel\/","title":{"rendered":"Extracting numbers from strings in Excel"},"content":{"rendered":"\n<p>Stata&#8217;s great at taking raw numbers and chugging out graphs with minimal edits. Often times you&#8217;ll get results that aren&#8217;t raw numbers, but instead will exist as a string. Instead, of getting:<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:45% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"240\" height=\"58\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/image.png\" alt=\"\" class=\"wp-image-302\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<p>&#8230;you&#8217;ll get <\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:22% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"132\" height=\"56\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/hr-as-string.png\" alt=\"\" class=\"wp-image-304\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<p>My previous strategy has been to manually extract these numbers into rows and columns. I just came across these two pages of Excel strategies that will pluck the numbers in the second picture and produce the first one!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Split strings into separate columns. <\/h4>\n\n\n\n<p>Reference: <a href=\"https:\/\/support.office.com\/en-us\/article\/split-text-into-different-columns-with-functions-49ec57f9-3d5a-44b2-82da-50dded6e4a68\">https:\/\/support.office.com\/en-us\/article\/split-text-into-different-columns-with-functions-49ec57f9-3d5a-44b2-82da-50dded6e4a68<\/a> <\/p>\n\n\n\n<p>Here, we&#8217;ll split the string at the spaces. There are two spaces, so you&#8217;ll end up with three new variables.  First, make three new &#8216;temp&#8217; columns to the right.  You&#8217;ll need a different code for each section&#8217;s destination cell.<\/p>\n\n\n\n<p>Left bit, or &#8220;0.80&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=LEFT(A2, SEARCH(\" \",A2,1))<\/code><\/pre>\n\n\n\n<p>Middle bit, or &#8220;(0.70,&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=MID(A2,SEARCH(\" \",A2,1)+1,SEARCH(\" \",A2,SEARCH(\" \",A2,1)+1)-SEARCH(\" \",A2,1))<\/code><\/pre>\n\n\n\n<p>Right bit, or &#8220;0.90)&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=RIGHT(A2,LEN(A2)-SEARCH(\" \",A2,SEARCH(\" \",A2,1)+1))<\/code><\/pre>\n\n\n\n<p>This assumes that your string of interest is sitting cell A2. Change this cell reference as needed. If all goes well, you should have new cells that look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"398\" height=\"61\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/image-1.png\" alt=\"\" class=\"wp-image-305\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/image-1.png 398w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/image-1-300x46.png 300w\" sizes=\"auto, (max-width: 398px) 100vw, 398px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Pluck numbers from strings. <\/h4>\n\n\n\n<p>Reference:  <a href=\"https:\/\/www.ablebits.com\/office-addins-blog\/2017\/11\/22\/excel-extract-number-from-string\/\">https:\/\/www.ablebits.com\/office-addins-blog\/2017\/11\/22\/excel-extract-number-from-string\/<\/a> <\/p>\n\n\n\n<p>Welp, there&#8217;s still some non-numeric text here. Time to pluck out the raw numbers! We&#8217;ll pretend that the &#8216;hrtemp&#8217; cell also has a non-numerical character in it (e.g., a percent sign) for completeness&#8217; sake. (Excel actually considers it a string still, which is why it&#8217;s not showing as &#8220;0.8&#8221;). Make 3 new rows to the right and use this code to extract the raw numbers contained in the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=(SUMPRODUCT(MID(0&amp;B2, LARGE(INDEX(ISNUMBER(--MID(B2, ROW(INDIRECT(\"1:\"&amp;LEN(B2))), 1)) * ROW(INDIRECT(\"1:\"&amp;LEN(B2))), 0), ROW(INDIRECT(\"1:\"&amp;LEN(B2))))+1, 1) * 10^ROW(INDIRECT(\"1:\"&amp;LEN(B2)))\/10))\/100<\/code><\/pre>\n\n\n\n<p>&#8230;obviously, you&#8217;ll need to change the B2 cell to C2 and D2 as needed. You should get: <\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:72% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"53\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/finished-hr-extraction.png\" alt=\"\" class=\"wp-image-308\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/finished-hr-extraction.png 572w, https:\/\/blog.uvm.edu\/tbplante\/files\/2019\/08\/finished-hr-extraction-300x28.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<p>Boom! You should be able to copy to strings in subsequent rows by hovering over the bottom right of each cell and dragging down. Now you can &#8220;import excel&#8221; in stata and use your hr, low95, and high95 variables!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stata&#8217;s great at taking raw numbers and chugging out graphs with minimal edits. Often times you&#8217;ll get results that aren&#8217;t raw numbers, but instead will exist as a string. Instead, of getting: &#8230;you&#8217;ll get My previous strategy has been to manually extract these numbers into rows and columns. I just came across these two pages &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2019\/08\/02\/extracting-numbers-from-strings-in-excel\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extracting numbers from strings in Excel<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":5,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":570,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/301\/revisions\/570"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}