<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extracting year ranges from character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864069#M341258</link>
    <description>&lt;P&gt;Example data as data step, or at least plain text so we can create a data set.&lt;/P&gt;
&lt;P&gt;You do not say what you expect to have for output for the "people who reported more than one year ranges".&lt;/P&gt;
&lt;P&gt;Doesn't work is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;/&amp;gt;" to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;/&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I bet that you have some conversion of numeric to character notes. Probably from this line as an example which is why request logs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living= "36-52";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;421  data example;
422     p1q1_living = "19-33 years, 36-52 years";
423     If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living=
423! "36-52";
424  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      423:66
NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      423:74
NOTE: Invalid numeric data, '19-33' , at line 423 column 66.
p1q1_living=0 _ERROR_=1 _N_=1
NOTE: The data set WORK.EXAMPLE has 1 observations and 1 variables.

&lt;/PRE&gt;
&lt;P&gt;Column 66 is the "19-33" after the "then p1q1_living=" and happens because the AND that follows is treated as a LOGICAL comparison, which in SAS are numeric 1/0 values for true/false. The second conversion at column 74 is at the AND as trying to get the types to match.&lt;/P&gt;
&lt;P&gt;This is where you have to describe and better show us what you expect for a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Mar 2023 14:43:41 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-03-14T14:43:41Z</dc:date>
    <item>
      <title>Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/863982#M341227</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a character variable (p1q1_living) for which data was entered inconsistently and some respondents have picked two age ranges (sample SAS dataset attached). I need to find a way to extract the start and last years of the reported year ranges and put them together using either hyphen or "to". I couldn't come up with a proper string function. So, I tried the attached manual conversion but it at least didn't work for people who reported more than one year ranges.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help in advance.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Have.PNG" style="width: 586px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81508iB1EB9AD65C48090D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Have.PNG" alt="Have.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Want.PNG" style="width: 459px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81509iB9C1A04CD6A3020F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Want.PNG" alt="Want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data PRISM.p1q1_rawdata4; 
	Set PRISM.p1q1_rawdata3;
	If p1q1_living = "18 to 27 years" then p1q1_living= "18-27"; If p1q1_living = "19 to 35 years" then p1q1_living= "19-35";
 	If p1q1_living = "19 to 42 years" then p1q1_living= "19-42"; If p1q1_living = "19 to 50 years" then p1q1_living= "19-50"; 
	If p1q1_living = "20 to 32 years" then p1q1_living= "20-32"; If p1q1_living = "20 to 39 years" then p1q1_living= "20-39"; 
	If p1q1_living = "20 to 40 years" then p1q1_living= "20-40"; If p1q1_living = "21 to 30 years" then p1q1_living= "21-30"; 

	If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living= "36-52";
	If p1q1_living = "22-39, 39-41" then p1q1_living= "22-39" AND p1q1_living= "39-41";
	
	If p1q1_living = "21 to 39 years" then p1q1_living= "21-39"; If p1q1_living = "22 to 46 years" then p1q1_living= "22-46"; 
	If p1q1_living = "23 to 46 years" then p1q1_living= "23-46"; If p1q1_living = "24 to 38 years" then p1q1_living= "24-38"; 
	If p1q1_living = "24 to 43 years" then p1q1_living= "24-43"; If p1q1_living = "25 to 35 years" then p1q1_living= "25-35"; 
	If p1q1_living = "26 to 40 years" then p1q1_living= "26-40"; If p1q1_living = "26 to 56 years" then p1q1_living= "26-56"; 
	If p1q1_living = "27 to 46 years" then p1q1_living= "27-46"; If p1q1_living = "29 to 41 years" then p1q1_living= "29-41"; 
	If p1q1_living = "29 to 49 years" then p1q1_living= "29-49"; If p1q1_living = "32 to 40 years" then p1q1_living= "32-40"; 
	If p1q1_living = "32 to 43 years" then p1q1_living= "32-43"; If p1q1_living = "32 to 47 years" then p1q1_living= "32-47"; 
	If p1q1_living = "32 to 48 years" then p1q1_living= "32-48"; If p1q1_living = "34 to 43 years" then p1q1_living= "34-43"; 
	If p1q1_living = "35 to 45 years" then p1q1_living= "35-45"; If p1q1_living = "45 to 62 years" then p1q1_living= "45-62"; 
	If p1q1_living = "18 to 28" then p1q1_living= "18-28";	If p1q1_living = "22 to 42" then p1q1_living= "22-42";
	If p1q1_living = "23 to 43" then p1q1_living= "23-43";	If p1q1_living = "24 to 38" then p1q1_living= "24-38";
	If p1q1_living = "25 to 37" then p1q1_living= "25-37";	If p1q1_living = "28 to 43" then p1q1_living= "28-43";
	If p1q1_living = "30 to 43" then p1q1_living= "30-43";	If p1q1_living = "31 to 51" then p1q1_living= "31-51";
	If p1q1_living = "ASKU" or p1q1_living= "NA" OR p1q1_living= "REF" then p1q1_living= " "; 

Run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;p1q1_living&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;12007c&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;17-35&lt;/STRONG&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;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 09:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/863982#M341227</guid>
      <dc:creator>Wub_SAS</dc:creator>
      <dc:date>2023-03-14T09:32:20Z</dc:date>
    </item>
    <item>
      <title>Extracting parts of a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/863978#M341229</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a character variable (p1q1_living) for which data was entered inconsistently and some respondents have picked two age ranges (sample SAS dataset attached). I need to find a way to extract the start and last years of the reported year ranges and put them together using either hyphen or "to". I couldn't come up with a proper string function. So, I tried the attached manual conversion but it at least didn't work for people who reported more than one year ranges.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Have.PNG" style="width: 131px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81506iCB396A6AEDE718DD/image-size/small?v=v2&amp;amp;px=200" role="button" title="Have.PNG" alt="Have.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Want.PNG" style="width: 124px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81507i480986C14E155FB5/image-size/small?v=v2&amp;amp;px=200" role="button" title="Want.PNG" alt="Want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data p1q1_rawdata4; 
	Set PRISM.p1q1_rawdata3;
	If p1q1_living = "18 to 27 years" then p1q1_living= "18-27"; If p1q1_living = "19 to 35 years" then p1q1_living= "19-35";
 	If p1q1_living = "19 to 42 years" then p1q1_living= "19-42"; If p1q1_living = "19 to 50 years" then p1q1_living= "19-50"; 
	If p1q1_living = "20 to 32 years" then p1q1_living= "20-32"; If p1q1_living = "20 to 39 years" then p1q1_living= "20-39"; 
	If p1q1_living = "20 to 40 years" then p1q1_living= "20-40"; If p1q1_living = "21 to 30 years" then p1q1_living= "21-30"; 

	If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living= "36-52";
	If p1q1_living = "22-39, 39-41" then p1q1_living= "22-39" AND p1q1_living= "39-41";
	
	If p1q1_living = "21 to 39 years" then p1q1_living= "21-39"; If p1q1_living = "22 to 46 years" then p1q1_living= "22-46"; 
	If p1q1_living = "23 to 46 years" then p1q1_living= "23-46"; If p1q1_living = "24 to 38 years" then p1q1_living= "24-38"; 
	If p1q1_living = "24 to 43 years" then p1q1_living= "24-43"; If p1q1_living = "25 to 35 years" then p1q1_living= "25-35"; 
	If p1q1_living = "26 to 40 years" then p1q1_living= "26-40"; If p1q1_living = "26 to 56 years" then p1q1_living= "26-56"; 
	If p1q1_living = "27 to 46 years" then p1q1_living= "27-46"; If p1q1_living = "29 to 41 years" then p1q1_living= "29-41"; 
	If p1q1_living = "29 to 49 years" then p1q1_living= "29-49"; If p1q1_living = "32 to 40 years" then p1q1_living= "32-40"; 
	If p1q1_living = "32 to 43 years" then p1q1_living= "32-43"; If p1q1_living = "32 to 47 years" then p1q1_living= "32-47"; 
	If p1q1_living = "32 to 48 years" then p1q1_living= "32-48"; If p1q1_living = "34 to 43 years" then p1q1_living= "34-43"; 
	If p1q1_living = "35 to 45 years" then p1q1_living= "35-45"; If p1q1_living = "45 to 62 years" then p1q1_living= "45-62"; 
	If p1q1_living = "18 to 28" then p1q1_living= "18-28";	If p1q1_living = "22 to 42" then p1q1_living= "22-42";
	If p1q1_living = "23 to 43" then p1q1_living= "23-43";	If p1q1_living = "24 to 38" then p1q1_living= "24-38";
	If p1q1_living = "25 to 37" then p1q1_living= "25-37";	If p1q1_living = "28 to 43" then p1q1_living= "28-43";
	If p1q1_living = "30 to 43" then p1q1_living= "30-43";	If p1q1_living = "31 to 51" then p1q1_living= "31-51";
	If p1q1_living = "ASKU" or p1q1_living= "NA" OR p1q1_living= "REF" then p1q1_living= " "; 

Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Mar 2023 09:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/863978#M341229</guid>
      <dc:creator>Wub_SAS</dc:creator>
      <dc:date>2023-03-14T09:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864069#M341258</link>
      <description>&lt;P&gt;Example data as data step, or at least plain text so we can create a data set.&lt;/P&gt;
&lt;P&gt;You do not say what you expect to have for output for the "people who reported more than one year ranges".&lt;/P&gt;
&lt;P&gt;Doesn't work is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;/&amp;gt;" to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;/&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I bet that you have some conversion of numeric to character notes. Probably from this line as an example which is why request logs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living= "36-52";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;421  data example;
422     p1q1_living = "19-33 years, 36-52 years";
423     If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living=
423! "36-52";
424  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      423:66
NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      423:74
NOTE: Invalid numeric data, '19-33' , at line 423 column 66.
p1q1_living=0 _ERROR_=1 _N_=1
NOTE: The data set WORK.EXAMPLE has 1 observations and 1 variables.

&lt;/PRE&gt;
&lt;P&gt;Column 66 is the "19-33" after the "then p1q1_living=" and happens because the AND that follows is treated as a LOGICAL comparison, which in SAS are numeric 1/0 values for true/false. The second conversion at column 74 is at the AND as trying to get the types to match.&lt;/P&gt;
&lt;P&gt;This is where you have to describe and better show us what you expect for a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 14:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864069#M341258</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-14T14:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864231#M341320</link>
      <description>&lt;P&gt;This code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	If p1q1_living = "19-33 years, 36-52 years" then p1q1_living= "19-33" AND p1q1_living= "36-52";
	If p1q1_living = "22-39, 39-41" then p1q1_living= "22-39" AND p1q1_living= "39-41";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;obviously does not work as intended. You cannot assign two values to the same variable at the same time. What it does is this: It assigns (or tries to assign) a boolean value. On the left side of the AND is the character value "22-39" which SAS tries to convert to a number, returning a missing value. On the right side is the expression&amp;nbsp; p1q1_living= "39-41", which evaluates to 0 (false). So the expression evaluates to . AND 0, which again evaluates to 0. Your variable finally gets the value "0", as the result is converted to character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you really want do do? If you want both values, there are two possibilities:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a new variable, which contains the second value, e.g.
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; If p1q1_living = "22-39, 39-41" then do;
  p1q1_living= "22-39";
  p1q1_living2= "39-41";
  end;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Output the second value to another observation:
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; If p1q1_living = "22-39, 39-41" then do;
  p1q1_living= "22-39";
  output;
  p1q1_living= "39-41";
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
if you go with this solution, remember to put another OUTPUT statement at the end of your datastep.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you just want to get the first age and the last age, (e.g.&amp;nbsp;"19-33 years, 36-52 years" becomes "19-52"), use SCAN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;p1q1_living = cats(scan(p1q1_living,1,' -','A'),'-',scan(p1q1_living,-1,' -,','A'));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(by adding the 'A' modifier to the scan function, we also consider alphabetical characters delimiters, so that only the numbers are considered words)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 09:39:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864231#M341320</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-15T09:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864244#M341323</link>
      <description>&lt;P&gt;Below how this could work for the sample data you've provided.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are two date ranges then the code will create two observations. If you want a different treatment for such cases then you need to tell us exactly what the desired result should be.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dlm='|';
  input ID:$10. p1q1_living:$30.;
  datalines;
12007c|17-35
12024x|
12024c|18 to 27 years
12025c|18-25
12032c|18-30
12033c|18-45
12043c|19 to 50 years
12044|19-27
12045c|19-33 years, 36-52 years
12048c|20 to 40 years
12049c|20-34
12314c|21 to 39 years
;

data want;
  set have;
  length cleansed $10;

  do _i=1 by 1;
    cleansed=scan(p1q1_living,_i,',');
    if _i&amp;gt;1 and missing(cleansed) then leave;
    cleansed=prxchange('s/^(\d+)[^\d]+(\d+).*[^\d]/$1-$2/oi',-1,strip(cleansed));
    output;
  end;
  drop _i;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1678880178243.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81552iC112E6EDD1DA5232/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1678880178243.png" alt="Patrick_0-1678880178243.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 11:38:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864244#M341323</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-15T11:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864264#M341336</link>
      <description>&lt;P&gt;An alternative to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;'s regular experssions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ 6. p1q1_living $ 8-40;
  dumy_variable= _N_; /* just for obs number */
cards;        
12007c 17-35         
12024c 18 to 27 years         
12025c 18-25         
12032c 18-30         
12033c 18-45         
12043c 19 to 50 years         
12044  19-27         
12045c 19-33 years, 36-52 years         
12048c 20 to 40 years         
12049c 20-34         
12314c 21 to 39 years
;
run;
proc print;
run;

data want;
  set have;
  p1q1_living=compress(p1q1_living,', -',"KD"); /* keep digits, commas, hyphens, and spaces only */
 
  do _N_=1 to countw(p1q1_living,",");
    p1q1_living_NEW=translate(compbl(strip(scan(p1q1_living,_N_,","))),"-"," ");
    output;
  end;
run;
proc print;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 12:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864264#M341336</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-03-15T12:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting year ranges from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864267#M341338</link>
      <description>&lt;PRE&gt;/*Same idea*/
data have;
  infile datalines truncover dlm='|';
  input ID:$10. p1q1_living:$30.;
  datalines;
12007c|17-35
12024x|
12024c|18 to 27 years
12025c|18-25
12032c|18-30
12033c|18-45
12043c|19 to 50 years
12044|19-27
12045c|19-33 years, 36-52 years
12048c|20 to 40 years
12049c|20-34
12314c|21 to 39 years
;

data want;
 set have;
 n=countw(p1q1_living,,'kd');
 if mod(n,2)=0 and n ne 0 then do;
 do i=1 to n by 2;
  want=catx('-',scan(p1q1_living,i,,'kd'),scan(p1q1_living,i+1,,'kd'));
  output;
 end;
 end;
 else output;
 
drop n i;
run;

proc print noobs;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1678885269316.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81559i897A985E3AD5FA7B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1678885269316.png" alt="Ksharp_0-1678885269316.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 13:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-year-ranges-from-character-variable/m-p/864267#M341338</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-15T13:01:26Z</dc:date>
    </item>
  </channel>
</rss>

