<?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: Applying new format using FORMAT and PUT in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590726#M15085</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279488"&gt;@marina_esp&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats are a powerful and very useful feature of SAS. As Jag and Paige have pointed out, a format doesn't change the internal value of a variable even if it is associated with the variable in a dataset. This has many advantages, e.g.:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You&amp;nbsp;&lt;STRONG&gt;save resources&lt;/STRONG&gt;, i.e., a lot of disk space and processing time, by storing only the short codes (regardless whether numeric or character) in the dataset and using the corresponding descriptive texts in format labels rather than separate, long character variables (whose values would possibly be repeated over and over again in a dataset).&lt;/LI&gt;
&lt;LI&gt;Formats &lt;STRONG&gt;facilitate maintenance&lt;/STRONG&gt;: It is easy to change a format label (e.g. "United States" --&amp;gt; "United States of America") in a format definition. You don't even need to touch the (possibly dozens of) datasets using the format to implement that change in your reports.&lt;/LI&gt;
&lt;LI&gt;Formats &lt;STRONG&gt;provide flexibility&lt;/STRONG&gt; because you can work with either the internal value (code) or the formatted value (be it from a permanently or temporarily associated format), just as needed.&lt;BR /&gt;Examples:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a format */

proc format;
value $code
'N1'='United States'
'N2'='Canada'
'S1'='Brazil'
'S2'='Argentina'
/* ... */
;
run;

/* Create test data */

data have;
input id c :$2. x;
format c $code.;
cards;
1 N2  50
2 S1  20
3 N1 100
4 S2  70
5 N1  60
;

/* Select data based on the internal values */

data south;
set have;
if c=:'S';
run;

/* Select data based on the formatted values */

data cdn;
set have(where=(put(c,$code.)='Canada'));
run;

/* Summarize data based on formatted values */

proc means data=have sum;
class c;
var x;
run;

/* Summarize data based on formatted values
   using a different format "on the fly" */

proc means data=have sum;
class c;
format c $1.;
var x;
run;

/* Frequency table sorted by internal values */

proc freq data=have;
tables c;
run;

/* Frequency table sorted alphabetically by formatted values */

proc freq data=have order=formatted;
tables c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Sun, 22 Sep 2019 12:23:29 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-09-22T12:23:29Z</dc:date>
    <item>
      <title>Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590709#M15079</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Dear SAS-users,&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;I&gt;I am very new to SAS&amp;nbsp;and&amp;nbsp;apologise for rather a basic question.&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;I&gt;I wonder what is the&amp;nbsp;difference between&amp;nbsp;applying a new format using FORMAT and creating a new variable with the new format using PUT?&amp;nbsp;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;I&gt;Below is the example of&amp;nbsp;custom-formatting ParcCode variable.&amp;nbsp;I tried both - using the FORMAT and create new variable Region using &amp;nbsp;PUT. In the&amp;nbsp;output table the both ParkCode and Region columns look identical, but the format of the Region is&amp;nbsp;blank.&amp;nbsp;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;I&gt;Is there any&amp;nbsp;crucial difference between&amp;nbsp;the two approaches?&amp;nbsp;I&amp;nbsp;understand that I'll need PUT to transform numeric to character, but in&amp;nbsp;this&amp;nbsp;case ParkCode is already a character, so I apparently can&amp;nbsp;just use FORMAT then?&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data np_lookup;
	retain FmtName "$RegLbl";
    set pg2.np_codeLookup (rename=(ParkCode = Start));
    if Region ne " " then Label = Region;
    	else Label = "Unknown";
    	keep start label fmtname;
run;

proc format cntlin=np_lookup;
run;

data np_endanger;
    set pg2.np_species;
    where Conservation_Status='Endangered';
    Region = put(parkcode, $reglbl.); 
    format parkcode $reglbl.  ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate some tips and links to resources where I can find an answer!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Marina&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2019 09:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590709#M15079</guid>
      <dc:creator>marina_esp</dc:creator>
      <dc:date>2019-09-22T09:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590710#M15080</link>
      <description>&lt;P&gt;If we apply the format with put function (1) then the region will have the values as per the format. However if you apply the format via format statement (2) then values will be assign superficially only for the display but internally it will still have the original values in region as like parkcode.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if you use put function with format then there is no need for format statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    1) Region = put(parkcode, $reglbl.); 
    2) format parkcode $reglbl.  ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Sep 2019 10:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590710#M15080</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-09-22T10:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590713#M15082</link>
      <description>&lt;P&gt;In addition to what was said by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable PARKCODE is NUMERIC, then the PUT statement changes it to character, and it contains the text as defined by the format. This could make a difference in some analyses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, using a FORMAT statement changes the appearance of the variable as us humans see it; it does not change the underlying value of the variable as SAS sees it. Using the PUT statement actually changes the values of a variable.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2019 11:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590713#M15082</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-22T11:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590720#M15084</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your answers! You explained perfectly and now I understand the critical difference between the two approached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again, many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Marina&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2019 11:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590720#M15084</guid>
      <dc:creator>marina_esp</dc:creator>
      <dc:date>2019-09-22T11:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590726#M15085</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279488"&gt;@marina_esp&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats are a powerful and very useful feature of SAS. As Jag and Paige have pointed out, a format doesn't change the internal value of a variable even if it is associated with the variable in a dataset. This has many advantages, e.g.:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You&amp;nbsp;&lt;STRONG&gt;save resources&lt;/STRONG&gt;, i.e., a lot of disk space and processing time, by storing only the short codes (regardless whether numeric or character) in the dataset and using the corresponding descriptive texts in format labels rather than separate, long character variables (whose values would possibly be repeated over and over again in a dataset).&lt;/LI&gt;
&lt;LI&gt;Formats &lt;STRONG&gt;facilitate maintenance&lt;/STRONG&gt;: It is easy to change a format label (e.g. "United States" --&amp;gt; "United States of America") in a format definition. You don't even need to touch the (possibly dozens of) datasets using the format to implement that change in your reports.&lt;/LI&gt;
&lt;LI&gt;Formats &lt;STRONG&gt;provide flexibility&lt;/STRONG&gt; because you can work with either the internal value (code) or the formatted value (be it from a permanently or temporarily associated format), just as needed.&lt;BR /&gt;Examples:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a format */

proc format;
value $code
'N1'='United States'
'N2'='Canada'
'S1'='Brazil'
'S2'='Argentina'
/* ... */
;
run;

/* Create test data */

data have;
input id c :$2. x;
format c $code.;
cards;
1 N2  50
2 S1  20
3 N1 100
4 S2  70
5 N1  60
;

/* Select data based on the internal values */

data south;
set have;
if c=:'S';
run;

/* Select data based on the formatted values */

data cdn;
set have(where=(put(c,$code.)='Canada'));
run;

/* Summarize data based on formatted values */

proc means data=have sum;
class c;
var x;
run;

/* Summarize data based on formatted values
   using a different format "on the fly" */

proc means data=have sum;
class c;
format c $1.;
var x;
run;

/* Frequency table sorted by internal values */

proc freq data=have;
tables c;
run;

/* Frequency table sorted alphabetically by formatted values */

proc freq data=have order=formatted;
tables c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Sun, 22 Sep 2019 12:23:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590726#M15085</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-22T12:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590750#M15086</link>
      <description>&lt;P&gt;Hello Reinhard,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for explanations and examples - I tried them all. I see how FORMAT can be more useful than PUT.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I ask you a question about one of the examples you gave?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cdn;
set have(where=(put(c,$code.)='Canada'));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here, I am not sure why do you have to use PUT when we already specified the FORMAT of column "c"&amp;nbsp;when we were creating dataset "have"? Is that because FORMAT did not change the native format of values in "c"&amp;nbsp;&amp;nbsp;, but only their apprearace in the reports, right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for answering!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;M&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;</description>
      <pubDate>Sun, 22 Sep 2019 17:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590750#M15086</guid>
      <dc:creator>marina_esp</dc:creator>
      <dc:date>2019-09-22T17:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590751#M15087</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279488"&gt;@marina_esp&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cdn;
set have(where=(put(c,$code.)='Canada'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here, I am not sure why do you have to use PUT when we already specified the FORMAT of column "c"&amp;nbsp;when we were creating dataset "have"? Is that because FORMAT did not change the native format of values in "c"&amp;nbsp;&amp;nbsp;, but only their apprearace in the reports, right?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You raise a very good point. It is indeed somewhat counterintuitive that the format name must be specified again in this WHERE condition although it is stored in dataset HAVE and SAS should be able to retrieve it from there. Of course, we need to specify somehow that we want to refer to the &lt;EM&gt;formatted&lt;/EM&gt; value of variable C because, as you've noticed correctly, the format did not change the "internal" value of the variable, so that the expression &lt;FONT face="courier new,courier"&gt;C='Canada'&lt;/FONT&gt; would be &lt;EM&gt;false&lt;/EM&gt;. (Note, however, how tempting it is to use this type of criterion if you saw the formatted values of C, e.g., in PROC PRINT output, &lt;EM&gt;without being aware&lt;/EM&gt; that format &lt;FONT face="courier new,courier"&gt;$code.&lt;/FONT&gt; is associated with that variable!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are ways to refer to the formatted value without specifying the format: by using SAS&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n01f5qrjoh9h4hn1olbdpb5pr2td.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;functions&lt;/A&gt; of category "Variable Information," e.g., the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p06uh7sqlh94nxn1gezyms3e9njn.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;VVALUE function&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cdn;
set have;
if vvalue(c)='Canada';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is possibly a better example than the previous one. Unfortunately, these "V..." functions cannot be used in WHERE conditions.* That's why I switched to IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*The error message in this case is really funny:&lt;/P&gt;
&lt;PRE&gt;76   data cdn;
77   set have;
78   where vvalue(c)='Canada';
&lt;FONT color="#FF0000"&gt;ERROR: Function VVALUE is supported by the DATA STEP only.&lt;/FONT&gt;
79   run;

NOTE: The SAS System stopped processing this step because of errors.&lt;/PRE&gt;
&lt;P&gt;As if this weren't a DATA STEP! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2019 17:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590751#M15087</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-22T17:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Applying new format using FORMAT and PUT</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590755#M15088</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ok, I see. I have even tried to use c ="Canada " and got an empty data frame as an output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for mentioning the VVALUE function, I've never heard about that, but will check it out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Today is the first time I posted a question on the SAS community, and I learned so much.&lt;/P&gt;&lt;P&gt;Thanks to you and the other guys who replied me earlier today.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS online communities are great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2019 18:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Applying-new-format-using-FORMAT-and-PUT/m-p/590755#M15088</guid>
      <dc:creator>marina_esp</dc:creator>
      <dc:date>2019-09-22T18:49:28Z</dc:date>
    </item>
  </channel>
</rss>

