<?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: check field for valid state name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260865#M50667</link>
    <description>&lt;P&gt;Why not use an informat instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
invalue $stateFmt (default=2 just upcase)
"AL", "AK", "AZ", "AR", "CA" = [$upcase2.]
other = "??";
run;

data test;
length code stateCode $2;
input code;
stateCode = input(code, $stateFmt.);
codeError = stateCode = "??";
datalines;
al
Ak
  AZ
AB
;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 02 Apr 2016 02:13:00 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-04-02T02:13:00Z</dc:date>
    <item>
      <title>check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260814#M50654</link>
      <description>&lt;P&gt;I am checking a field for a valid postal code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'Property State' IN ( "AL", "AK", "AZ", "AR", "CA", etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is setting this up in a macro faster than having it in an if/then statement?&amp;nbsp; I'm checking a file with a couple million rows and want to make sure I'm efficient as possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance, have a great weekend.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2016 19:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260814#M50654</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2016-04-01T19:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260828#M50656</link>
      <description>&lt;P&gt;I'm curious, What would be the macro version of this?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2016 20:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260828#M50656</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-01T20:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260831#M50657</link>
      <description>&lt;P&gt;I did some testing and the use of INDEX() proved quite fast. I cannot imagine that looping through an array of 52 states for every observation would be faster. But ultimately the proof of the pudidng is in the eating.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;states="AL|AK|AR|AZ|CA|...|WV|WY";
if (index(states, statecode)) &amp;gt; 0 then put 'State OK';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain the variable states and assign only when _N_=1 for even better performance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general this class of problems (table lookup) can be addressed multiple ways:&lt;/P&gt;
&lt;P&gt;1) Format&lt;/P&gt;
&lt;P&gt;2) Hash table&lt;/P&gt;
&lt;P&gt;3) SQL&lt;/P&gt;
&lt;P&gt;4) prxmatch (I tried; wasslower)&lt;/P&gt;
&lt;P&gt;5) if/then/else&lt;/P&gt;
&lt;P&gt;6) Character functions&lt;/P&gt;
&lt;P&gt;7) ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This time I choose 6) but 1) and 2) do in-memory searches and can be quite fast as well. They do require more programming. It is worth trying though. They usually work best when the list of values is in a dataset. But unless the USA plans to acquire new states or spin off existing ones any time soon the list should be quite stable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2016 20:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260831#M50657</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-01T20:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260833#M50659</link>
      <description>&lt;P&gt;If I were using something like this very often I would set up a custom format. Possibly&amp;nbsp; with a result of 'Valid' for valid codes and 'Invalid' for anything else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;/P&gt;
&lt;P&gt;value &amp;amp;customformat&lt;/P&gt;
&lt;P&gt;&amp;nbsp;"AL", "AK", "AZ", "AR", "CA", etc = "Valid"&lt;/P&gt;
&lt;P&gt;other = "Invalid"&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the code could be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If put(upcase(variable),$customformat.) ='Valid' then do;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;Else do;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the approach to catch site codes that get misentered OR get added to data without forewarning.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2016 20:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260833#M50659</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-01T20:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260846#M50663</link>
      <description>&lt;P&gt;What version of SAS are you on?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Before 9.4 there appears to be some SAS out of the box formats for State Names/Codes that may be available to you, but they're gone in SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would echo the format solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe one of these:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000211303.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000211303.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2016 22:15:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260846#M50663</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-01T22:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260865#M50667</link>
      <description>&lt;P&gt;Why not use an informat instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
invalue $stateFmt (default=2 just upcase)
"AL", "AK", "AZ", "AR", "CA" = [$upcase2.]
other = "??";
run;

data test;
length code stateCode $2;
input code;
stateCode = input(code, $stateFmt.);
codeError = stateCode = "??";
datalines;
al
Ak
  AZ
AB
;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Apr 2016 02:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260865#M50667</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-02T02:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260873#M50669</link>
      <description>&lt;PRE&gt;
Check functions:
If it return missing value, then it should be invalid State Name.


data _NULL_;
fips=stfips ('NC');
put fips=; 
state=stname('NC');
put state=; 
state=stname('AA');
put state=; 
run;


&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Apr 2016 03:39:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/260873#M50669</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-02T03:39:21Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261133#M50752</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Why not use an informat instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used an informat for a while with one project&amp;nbsp;but when I went back to the data supplier I then had to re-read the data to get the original unexpected value(s) and associated record identifiers&amp;nbsp;to show them what the unexpected values were. By using the format with a report step I could then use a where statement in proc print to list the same information easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Volume of records (relatively low)&amp;nbsp;and project requirements (very clean data) made the format approach a better fit in my case.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 17:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261133#M50752</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-04T17:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261149#M50761</link>
      <description>&lt;P&gt;You are right&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;, if you use the informat to read the data, you lose the original information and that can lead to some problems later. That's why I created a new variable with the input function instead of using my&amp;nbsp;informat within the input statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The informat approach nevertheless allows you to hide the UPCASE and LEFT operations in the format, and to exploit the _ERROR_ mechanism, if you wish.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 17:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261149#M50761</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-04T17:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261242#M50793</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;and I should have spent a bit of time realizing that not everyone is looking at dozens of variables with custom formats/informats. I mostly worry about informat when reading the data but could use this for a report approach as well.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 21:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261242#M50793</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-04T21:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: check field for valid state name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261355#M50842</link>
      <description>&lt;P&gt;Other than using a macro variable so I don't have a long in() clause I have no idea.&amp;nbsp; Looking for anything to be more efficient.&amp;nbsp; There's a large amount of records that I need to check.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 12:45:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-field-for-valid-state-name/m-p/261355#M50842</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2016-04-05T12:45:21Z</dc:date>
    </item>
  </channel>
</rss>

