<?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: Create new variable when Variable is between 2 numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877437#M346639</link>
    <description>&lt;P&gt;Is P_code a numeric or character variable?&lt;/P&gt;
&lt;P&gt;This is the sort of thing that a Format works well for as formats take the logic out of the data stream and can be reused easily.&lt;/P&gt;
&lt;P&gt;This assumes your value is character otherwise leading zero doesn't mean much. If your P_code is numeric then remove the $ from the name of the format: Code2State, and remove all of the quotes in value lists. Typically "between" can be problematic with character values but with all of these 4 characters there shouldn't be any problem. Note that you could also include a keyword Other to set a value if you get an unexpected value of P-code. That would be a simple Other = 'Not valid code' or similar.&lt;/P&gt;
&lt;PRE&gt;proc format;
value $code2state
'1000'-'1999', 
'2000'-'2599',
'2619'-'2899',
'2921'-'2999' = 'NSW'
'0200'-'0299', 
'2600'-'2618',
'2900'-'2920' = 'ACT'

'3000'-'3999',
'8000'-'8999' = 'VIC'

'4000'-'4999',
'9000'-'9999' = 'QLD'

'5000'-'5799',
'5800'-'5999' = 'SA'

'6000'-'6797',
'6800'-'6999' = 'WA'

'7000'-'7799',
'7800'-'7999' = 'TAS'

'0800'-'0899',
'0900'-'0999' = 'NT'
;
run;
&lt;/PRE&gt;
&lt;P&gt;And how to use to create new variable.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   State = put(p_code,$code2state.);
run;&lt;/PRE&gt;
&lt;P&gt;Actually you could just use the format with P_code in procs to display the state abbreviation.&lt;/P&gt;
&lt;P&gt;You could also make another format with the full name of the state for the times that my be useful.&lt;/P&gt;
&lt;P&gt;I use our US Zip&amp;nbsp; postal code to look up missing city names, county names and can create custom reporting areas such as "north side of city X" and "south side of city X" but does require having some information about the relationships.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats are a very powerful tool in SAS. You don't have to create new variables to change the text of reports or groups of an analysis procedure. The groups created by formats are used in report or analysis procedures and generally for graphing purposes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read the documentation for limits. The format must be available in a session. So either re-execute the code in each session or create a format catalog in a permanent library and point to that location with the FMTSEARCH&amp;nbsp; option.&lt;/P&gt;</description>
    <pubDate>Thu, 25 May 2023 04:07:43 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-05-25T04:07:43Z</dc:date>
    <item>
      <title>Create new variable when Variable is between 2 numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877422#M346631</link>
      <description>&lt;P&gt;Have have a variable called P_Code which has postcodes for customers.&lt;/P&gt;&lt;P&gt;So something like.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if P_Code between 1000-1999 then State="NSW"&lt;/P&gt;&lt;P&gt;if P_Code between 2000-2599 then State="NSW"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is a table of Australian postcodes and their States.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;State/Territory Abbreviation Postcode range&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="New South Wales" href="https://en.wikipedia.org/wiki/New_South_Wales" target="_blank" rel="noopener"&gt;New South Wales&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;NSW&lt;/TD&gt;&lt;TD&gt;1000—1999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;BR /&gt;2000—2599&lt;BR /&gt;2619—2899&lt;BR /&gt;2921—2999&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Australian Capital Territory" href="https://en.wikipedia.org/wiki/Australian_Capital_Territory" target="_blank" rel="noopener"&gt;Australian Capital Territory&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;ACT&lt;/TD&gt;&lt;TD&gt;0200—0299&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;BR /&gt;2600—2618&lt;BR /&gt;2900—2920&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Victoria (Australia)" href="https://en.wikipedia.org/wiki/Victoria_(Australia)" target="_blank" rel="noopener"&gt;Victoria&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;VIC&lt;/TD&gt;&lt;TD&gt;3000—3999&lt;BR /&gt;8000—8999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Queensland" href="https://en.wikipedia.org/wiki/Queensland" target="_blank" rel="noopener"&gt;Queensland&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;QLD&lt;/TD&gt;&lt;TD&gt;4000—4999&lt;BR /&gt;9000—9999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="South Australia" href="https://en.wikipedia.org/wiki/South_Australia" target="_blank" rel="noopener"&gt;South Australia&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;SA&lt;/TD&gt;&lt;TD&gt;5000—5799&lt;BR /&gt;5800—5999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Western Australia" href="https://en.wikipedia.org/wiki/Western_Australia" target="_blank" rel="noopener"&gt;Western Australia&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;WA&lt;/TD&gt;&lt;TD&gt;6000—6797&lt;BR /&gt;6800—6999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Tasmania" href="https://en.wikipedia.org/wiki/Tasmania" target="_blank" rel="noopener"&gt;Tasmania&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;TAS&lt;/TD&gt;&lt;TD&gt;7000—7799&lt;BR /&gt;7800—7999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only)&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;A title="Northern Territory" href="https://en.wikipedia.org/wiki/Northern_Territory" target="_blank" rel="noopener"&gt;Northern Territory&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;NT&lt;/TD&gt;&lt;TD&gt;0800—0899&lt;BR /&gt;0900—0999&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;(LVRs and PO Boxes only&lt;/I&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 25 May 2023 01:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877422#M346631</guid>
      <dc:creator>Haydo</dc:creator>
      <dc:date>2023-05-25T01:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable when Variable is between 2 numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877433#M346636</link>
      <description>I have used the below. Don't know why I didn't think of this before posting. Sorry if I wasted anyone's time.&lt;BR /&gt;&lt;BR /&gt;if P_Code &amp;gt;=0800 and P_Code &amp;lt;=0999 then State"NT";</description>
      <pubDate>Thu, 25 May 2023 03:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877433#M346636</guid>
      <dc:creator>Haydo</dc:creator>
      <dc:date>2023-05-25T03:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable when Variable is between 2 numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877437#M346639</link>
      <description>&lt;P&gt;Is P_code a numeric or character variable?&lt;/P&gt;
&lt;P&gt;This is the sort of thing that a Format works well for as formats take the logic out of the data stream and can be reused easily.&lt;/P&gt;
&lt;P&gt;This assumes your value is character otherwise leading zero doesn't mean much. If your P_code is numeric then remove the $ from the name of the format: Code2State, and remove all of the quotes in value lists. Typically "between" can be problematic with character values but with all of these 4 characters there shouldn't be any problem. Note that you could also include a keyword Other to set a value if you get an unexpected value of P-code. That would be a simple Other = 'Not valid code' or similar.&lt;/P&gt;
&lt;PRE&gt;proc format;
value $code2state
'1000'-'1999', 
'2000'-'2599',
'2619'-'2899',
'2921'-'2999' = 'NSW'
'0200'-'0299', 
'2600'-'2618',
'2900'-'2920' = 'ACT'

'3000'-'3999',
'8000'-'8999' = 'VIC'

'4000'-'4999',
'9000'-'9999' = 'QLD'

'5000'-'5799',
'5800'-'5999' = 'SA'

'6000'-'6797',
'6800'-'6999' = 'WA'

'7000'-'7799',
'7800'-'7999' = 'TAS'

'0800'-'0899',
'0900'-'0999' = 'NT'
;
run;
&lt;/PRE&gt;
&lt;P&gt;And how to use to create new variable.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   State = put(p_code,$code2state.);
run;&lt;/PRE&gt;
&lt;P&gt;Actually you could just use the format with P_code in procs to display the state abbreviation.&lt;/P&gt;
&lt;P&gt;You could also make another format with the full name of the state for the times that my be useful.&lt;/P&gt;
&lt;P&gt;I use our US Zip&amp;nbsp; postal code to look up missing city names, county names and can create custom reporting areas such as "north side of city X" and "south side of city X" but does require having some information about the relationships.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats are a very powerful tool in SAS. You don't have to create new variables to change the text of reports or groups of an analysis procedure. The groups created by formats are used in report or analysis procedures and generally for graphing purposes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read the documentation for limits. The format must be available in a session. So either re-execute the code in each session or create a format catalog in a permanent library and point to that location with the FMTSEARCH&amp;nbsp; option.&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 04:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-when-Variable-is-between-2-numbers/m-p/877437#M346639</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-25T04:07:43Z</dc:date>
    </item>
  </channel>
</rss>

