<?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 retain character character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674873#M203253</link>
    <description>&lt;P&gt;Hi, I'm working on a sample data set using property information.&amp;nbsp; Some of the property records have missing fields like 'prpty_city' and 'prpty_zip'.&amp;nbsp; I'm attempting to associate similar property records with those that are missing these two fields by creating a field named 'prpty_street_bracket'.&amp;nbsp; I'm also trying to use retain to bring down the 'prpty_city' and 'prpty_zip' across observations that share the same 'prpty_street_bracket'.&amp;nbsp; However, I don't know exactly how retain works with character variables.&amp;nbsp; I've successfully used retain with numeric fields but not so much with character fields.&amp;nbsp; Here's my sample code.&amp;nbsp; Any ideas/suggestions would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andy B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=COLLIN;&lt;BR /&gt;BY prpty_street_bracket descending prpty_city descending prpty_zip;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;DATA COLLIN;&lt;BR /&gt;SET COLLIN;&lt;BR /&gt;BY prpty_street_bracket descending prpty_city descending prpty_zip;&lt;BR /&gt;FORMAT PCITY $50. PZIP $5.;&lt;/P&gt;
&lt;P&gt;RETAIN PCITY PZIP;&lt;/P&gt;
&lt;P&gt;IF FIRST.prpty_street_bracket&lt;BR /&gt;THEN DO;&lt;BR /&gt;PCITY = prpty_city;&lt;BR /&gt;PZIP = prpty_zip;&lt;BR /&gt;END;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 05 Aug 2020 22:11:56 GMT</pubDate>
    <dc:creator>agbpilot</dc:creator>
    <dc:date>2020-08-05T22:11:56Z</dc:date>
    <item>
      <title>retain character character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674873#M203253</link>
      <description>&lt;P&gt;Hi, I'm working on a sample data set using property information.&amp;nbsp; Some of the property records have missing fields like 'prpty_city' and 'prpty_zip'.&amp;nbsp; I'm attempting to associate similar property records with those that are missing these two fields by creating a field named 'prpty_street_bracket'.&amp;nbsp; I'm also trying to use retain to bring down the 'prpty_city' and 'prpty_zip' across observations that share the same 'prpty_street_bracket'.&amp;nbsp; However, I don't know exactly how retain works with character variables.&amp;nbsp; I've successfully used retain with numeric fields but not so much with character fields.&amp;nbsp; Here's my sample code.&amp;nbsp; Any ideas/suggestions would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andy B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=COLLIN;&lt;BR /&gt;BY prpty_street_bracket descending prpty_city descending prpty_zip;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;DATA COLLIN;&lt;BR /&gt;SET COLLIN;&lt;BR /&gt;BY prpty_street_bracket descending prpty_city descending prpty_zip;&lt;BR /&gt;FORMAT PCITY $50. PZIP $5.;&lt;/P&gt;
&lt;P&gt;RETAIN PCITY PZIP;&lt;/P&gt;
&lt;P&gt;IF FIRST.prpty_street_bracket&lt;BR /&gt;THEN DO;&lt;BR /&gt;PCITY = prpty_city;&lt;BR /&gt;PZIP = prpty_zip;&lt;BR /&gt;END;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Aug 2020 22:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674873#M203253</guid>
      <dc:creator>agbpilot</dc:creator>
      <dc:date>2020-08-05T22:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: retain character character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674875#M203254</link>
      <description>&lt;P&gt;Treat the value exactly the same as you would a numeric variable. Assign a value when you need it, set it missing when you don't need it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the code you show not working somehow? If so describe what is not working, best to provide data example and possibly the log.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Aug 2020 22:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674875#M203254</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-05T22:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: retain character character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674876#M203255</link>
      <description>&lt;P&gt;Here is a program that will collapse all data for a&amp;nbsp;&lt;SPAN&gt;prpty_street_bracket into a single observation.&amp;nbsp; This may or may not be an acceptable solution, so try it and see if you like it.&amp;nbsp; For ALL variables in the data set, it locates the last nonmissing value.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=collin;
   by prpty_street_bracket descending prpty_city descending prpty_zip;
run;

data maybe_i_will_like_this;
   update collin (obs=0) collin;
   by prty_street_bracket;
run;&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Wed, 05 Aug 2020 23:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/674876#M203255</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-08-05T23:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: retain character character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/675146#M203373</link>
      <description>&lt;P&gt;Hi, thanks for your reply and suggestion.&amp;nbsp; In my process since I am re-using the same data set name "COLLIN" across multiple steps, I ended up just having to run the program in its entirety and the output is now working as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One question about retain.&amp;nbsp; For numeric values, I typically initialize the value to zero.&amp;nbsp; Here in this example, I'm not explicitly initializing the character value.&amp;nbsp; However, the code is producing the desired outcome.&amp;nbsp; So I've left it alone..&amp;nbsp; But if I were to explicitly initialize these character variables, I guess I would want to initialize them to missing or ' '.&amp;nbsp; Is there a best practice for initializing character variables using retain?&amp;nbsp; i.e., would a character variable with a max length of $5. have to be initialized in the retain statement as '&amp;nbsp; &amp;nbsp; &amp;nbsp;' ?&amp;nbsp; Or could I just define the length of this variable being retained in the format statement as $5.?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andy&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 21:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/675146#M203373</guid>
      <dc:creator>agbpilot</dc:creator>
      <dc:date>2020-08-06T21:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: retain character character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/676183#M203846</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/87761"&gt;@agbpilot&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, thanks for your reply and suggestion.&amp;nbsp; In my process since I am re-using the same data set name "COLLIN" across multiple steps, I ended up just having to run the program in its entirety and the output is now working as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One question about retain.&amp;nbsp; For numeric values, I typically initialize the value to zero.&amp;nbsp; Here in this example, I'm not explicitly initializing the character value.&amp;nbsp; However, the code is producing the desired outcome.&amp;nbsp; So I've left it alone..&amp;nbsp; But if I were to explicitly initialize these character variables, I guess I would want to initialize them to missing or ' '.&amp;nbsp; Is there a best practice for initializing character variables using retain?&amp;nbsp; i.e., would a character variable with a max length of $5. have to be initialized in the retain statement as '&amp;nbsp; &amp;nbsp; &amp;nbsp;' ?&amp;nbsp; Or could I just define the length of this variable being retained in the format statement as $5.?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andy&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Default behavior for Retain and character values is to initialize to missing.&lt;/P&gt;
&lt;P&gt;If I have a specific value that I need in my data I initialize it in the Retain statement.&lt;/P&gt;
&lt;P&gt;I tend to define the length explicitly before any Retain statement. The default format for any character variable defined in a Length statement would the $nn. where nn is the length so the format is generally not needed. Order of statements matters and if you retain and initialize a character variable without a length statement prior will result in a length of the initial value.&amp;nbsp; Since counting multiple spaces to initialize blanks is awkward at best I leave that to the Length statement. Plus it is easier to see from the code what the intended length was because counting spaces ....&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 14:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-character-character-variable/m-p/676183#M203846</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-12T14:44:29Z</dc:date>
    </item>
  </channel>
</rss>

