<?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: use length, index, and substr for zipcode variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608593#M177145</link>
    <description>Thank you for sharing the link! I learned so much from this SAS community.  This question is exactly what my instructor wrote. I will explain to her about the format.</description>
    <pubDate>Sun, 01 Dec 2019 18:11:05 GMT</pubDate>
    <dc:creator>Amy0223</dc:creator>
    <dc:date>2019-12-01T18:11:05Z</dc:date>
    <item>
      <title>use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608579#M177133</link>
      <description>&lt;P&gt;Hi, I don't know if I'm answering the below question correctly. I tried but my codes just look weird. I'd like to hear your advice. Thank you very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Variable zipcode is read with format$10. It reads zip codes in the form 07417 or 07417-1280 Create variable_9digit. It equals 1 if zipcode has a hyphen separating the fifth and seven digits. Otherwise it equals 0. Write the statements three ways, using the length, index, ad substr functions.&lt;/P&gt;
&lt;PRE&gt;/* using length function*/
data zipcode;
length zipcode $10;
input zipcode ;
cards;
07417
07417-1280
;
run;
DATA zipcode1;
Set zipcode;
if zipcode= '07417-1280' then zipcode2= '1';
else zipcode2= '0';
proc print noobs;
title 'Using length function';
run;

/* using substr function*/
DATA zipcode2;
format zipcode $10.;
zipcode= '07417-1280';
form1 = Substr( zipcode, 1, 5);
if form1= '07417' then newform1='0';
form2 = Substr( zipcode, 1);
if form2= '07417-1280'  then newform2= '1' ;
proc print noobs;
title 'Using substr function';
RUN; 

/* using index function*/
Data zipcode3;
format zipcode $10.;
zipcode= '07417-1280';
form1 = Substr( zipcode,1, index(zipcode, '-')-1);
if form1= '07417'  then newform1= '0' ;
form2 = Substr( zipcode, 1);
if form2= '07417-1280'  then newform2= '1' ;
proc print noobs;
title 'Using index function';
RUN; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 15:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608579#M177133</guid>
      <dc:creator>Amy0223</dc:creator>
      <dc:date>2019-12-01T15:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608582#M177136</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265801"&gt;@Amy0223&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a typical use case for regular expressions.&lt;/P&gt;
&lt;P&gt;The function prxmatch() as written below checked if the zipcode variable match the following pattern: 5 digits (\d), 1 hyphen, 4 digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zipcode_flag;
	set zipcode;
	if prxmatch('/\d{5}\-\d{4}/',zipcode) then variable_9digit = 1;
	else variable_9digit = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; The issue with your 3 tests is that your code depends specifically on one zip code in particular and not in general.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 15:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608582#M177136</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-01T15:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608583#M177137</link>
      <description>Thank you for taking your time to check my code and   provide helpful feedback. I greatly appreciate  your help!</description>
      <pubDate>Sun, 01 Dec 2019 16:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608583#M177137</guid>
      <dc:creator>Amy0223</dc:creator>
      <dc:date>2019-12-01T16:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608584#M177138</link>
      <description>&lt;P&gt;Below is my updated codes, do you think this answers the problem?&lt;/P&gt;
&lt;PRE&gt;/* using length function*/
data zipcode;
length zipcode $10;
input zipcode ;
cards;
07417
07417-1280
;
run;

DATA zipcode1;
Set zipcode;
if prxmatch('/\d{5}\-\d{4}/',zipcode) then variable_9digit = 1;
else variable_9digit = 0;
proc print noobs;
title 'Using length function';
run;

/* using index function*/
data zipcode2;	
set zipcode;
if index(zipcode,'-') then variable_9digit = 1;
else variable_9digit=0;
proc print noobs;
title 'Using index function';
RUN; 

/* using substr function*/
DATA zipcode3;
set zipcode;
if Substr( zipcode, 1, 5) then variable_9digit = 1;
else variable_9digit = 0;
proc print noobs;
title 'Using substr function';
RUN; &lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Dec 2019 16:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608584#M177138</guid>
      <dc:creator>Amy0223</dc:creator>
      <dc:date>2019-12-01T16:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608585#M177139</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265801"&gt;@Amy0223&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand why you need to use the different functions separately, because in my opinion, what defines the zipcode pattern is the conjonction of&amp;nbsp;4 conditions:&lt;BR /&gt;- length of the zipcode = 10&amp;nbsp;&lt;BR /&gt;- digits 1 to 5 = a number&lt;BR /&gt;- digits 7 to 9 = a number&lt;BR /&gt;- 6th digit = an hyphen&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;The use of the prxmatch function is a more efficient way to do that but you can also use the traditional length(), index() and substr() functions to create the flag variable. It doesn't make sense to use them separately.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zipcode_check;
	set zipcode;

	if length(zipcode)= 10 and
	   0 &amp;lt; substr(zipcode, 1, 5) &amp;lt; 99999 and
	   0 &amp;lt; substr(zipcode, 7, 4) &amp;lt; 9999 and
	   index(zipcode,"-")= 6
	   
	   then variable_9digit=1;
	   
	else variable_9digit=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 17:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608585#M177139</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-01T17:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608586#M177140</link>
      <description>&lt;P&gt;You didn't use the LENGTH() function as the problem requested.&lt;/P&gt;
&lt;P&gt;You cannot use a character expression as if it was a boolean expression.&amp;nbsp; But you can use a numeric&amp;nbsp; expression since SAS will treat 0 (or missing) as FALSE and any other number as TRUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why are you making a character variable instead of a numeric one?&lt;/P&gt;
&lt;P&gt;Numeric is a lot easier since SAS will evaluate boolean expressions to 1 for TRUE and 0 for FALSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;variable_9digit = (  '-' = substr( zipcode, 6, 1) ) ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Dec 2019 17:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608586#M177140</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-01T17:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608590#M177142</link>
      <description>Wow, I didn't know I could use them together. I thought I had to use the length, index, ad substr functions separately because the question said to write the statements three ways. I was really confused. Thank you very much for showing me a more efficient way!</description>
      <pubDate>Sun, 01 Dec 2019 17:44:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608590#M177142</guid>
      <dc:creator>Amy0223</dc:creator>
      <dc:date>2019-12-01T17:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608591#M177143</link>
      <description>&lt;P&gt;Do you have access to SAS to test your programs? Actually trying is the best way to learn. Especially when your programs don't work as you will learn more from the mistakes than from the code you get right the first time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can download a copy for for free from SAS for use in learning.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sas.com/en_us/software/university-edition.html" target="_self"&gt;https://www.sas.com/en_us/software/university-edition.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 17:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608591#M177143</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-01T17:53:10Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608592#M177144</link>
      <description>&lt;P&gt;Did your instructor really write:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Variable zipcode is read with format$10.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;If so you can explain to them that&amp;nbsp; in SAS you use an INFORMAT to read text into values. FORMATS are used to convert values into text for display.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2019 17:57:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608592#M177144</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-01T17:57:27Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608593#M177145</link>
      <description>Thank you for sharing the link! I learned so much from this SAS community.  This question is exactly what my instructor wrote. I will explain to her about the format.</description>
      <pubDate>Sun, 01 Dec 2019 18:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608593#M177145</guid>
      <dc:creator>Amy0223</dc:creator>
      <dc:date>2019-12-01T18:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: use length, index, and substr for zipcode variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608595#M177146</link>
      <description>You're welcome &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265801"&gt;@Amy0223&lt;/a&gt;!&lt;BR /&gt;Could you please set the topic as answered so that it can be accessible to the community? Thank you</description>
      <pubDate>Sun, 01 Dec 2019 19:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-length-index-and-substr-for-zipcode-variable/m-p/608595#M177146</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-01T19:32:15Z</dc:date>
    </item>
  </channel>
</rss>

