<?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: get format whose name contains the value of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818165#M322947</link>
    <description>And thank you for the reminder that character formats start with a dollar sign. I'm always a bit confused by value/invalue, put/input; and now that I've learned of putc/putn...</description>
    <pubDate>Tue, 14 Jun 2022 17:30:37 GMT</pubDate>
    <dc:creator>Quodly</dc:creator>
    <dc:date>2022-06-14T17:30:37Z</dc:date>
    <item>
      <title>get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817758#M322785</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  x1 = "asdf";

  y = "1";
  z = vvaluex(cats("x", y)); /* get value of variable whose name contains value of another variable */
run;

/**/

%let x1 = asdf;
data b;
  y = "1";
  z = symget(cats("x", y)); /* get value of macro vaiable whose name contains value of a variable */
run;

/**/

proc format;
  value $ x1_fmt
    "asdf" = "fdsa"
  ;
run;

data c1;
  z = "asdf";
  format z x1_fmt.; /* this is what i want */
run;

data c2;
  y = "1";
  z = "asdf";
  format z function(cats("x", y, "_fmt")); /* is there such a function? */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;i know the function&amp;nbsp;&lt;EM&gt;vvaluex&lt;/EM&gt; for variables and the&amp;nbsp;function&lt;EM&gt; symget&lt;/EM&gt; for macro variables, as in the example above. is there a similar function for formats? i'd like to avoid&amp;nbsp;&lt;EM&gt;if-then&lt;/EM&gt; and&amp;nbsp;&lt;EM&gt;select-when&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 11:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817758#M322785</guid>
      <dc:creator>Quodly</dc:creator>
      <dc:date>2022-06-13T11:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817760#M322786</link>
      <description>&lt;P&gt;You could use a macro variable, but this will take two consecutive data steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c2;
  y = "1";
  call symputx('y',y);
run;
data c3;
  z = "asdf";
  format z x&amp;amp;y._fmt.; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jun 2022 12:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817760#M322786</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-13T12:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817764#M322789</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;not directly, since several formats could contain the value 'asdf'.&lt;/P&gt;
&lt;P&gt;In a dataset, the function vformat r&lt;SPAN&gt;eturns the format that is associated with the specified variable. But I bet you already know this.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You could write out the formats and search for the ones that contain the value you look for.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods listing;
proc format cntlout=work.myformats;
run;
%let seek=%upcase(asdf);
%let fmtnames=;
PROC SQL noprint;
   SELECT distinct FMTNAME INTO :fmtnames SEPARATED BY ' '
   FROM myformats
   WHERE index(upcase(strip(label)),"&amp;amp;seek.")
   OR index(upcase(start),"&amp;amp;seek.")
   OR index(upcase(end),"&amp;amp;seek.")
   ORDER BY fmtname
   ;
QUIT;
%put Formats containing "&amp;amp;seek.": &amp;amp;=fmtnames.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 12:16:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817764#M322789</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-06-13T12:16:57Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817811#M322811</link>
      <description>&lt;P&gt;Not for format unfortunately. For conversion, PUTC/PUTN work but you also need to know the variable type.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 14:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817811#M322811</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-13T14:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817816#M322814</link>
      <description>&lt;P&gt;You cannot modify the metadata of a dataset in during the execution of the step that is making the dataset.&lt;/P&gt;
&lt;P&gt;So if you want to modify the format attached to a variable based on the value in the data you will need at least two steps.&amp;nbsp; One to determine the name of the format and another to attach that format to a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could add a pre-processing step to determine the format and use that information to attach the format in the step that generates the dataset.&amp;nbsp; Or add a post processing step to modify the format that is attached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;&lt;BR /&gt; &amp;nbsp;call&amp;nbsp;symputx('fmt',cats('$x',y,'_fmt.'));&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you will have a macro variable named FMT with a name based on the LAST value of the variable Y read from the dataset HAVE.&amp;nbsp; You can use that to modify the metadata of the dataset WANT to indicate that Z should use that format to display its values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets nolist lib=work;
  modify want;
    format z &amp;amp;fmt ;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jun 2022 14:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/817816#M322814</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-13T14:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818066#M322900</link>
      <description>&lt;P&gt;thank you for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;my example is misleading for my actual intention: i want to use formats for table lookups. for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
  z1 = "asdf";
  z2 = put(z1, x1_fmt.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for that, i don't need to change the metadata of a data set. the format which i want to use for the table lookup depends on another variable (variable &lt;EM&gt;y&lt;/EM&gt; in my example). of course, i can just use&amp;nbsp;&lt;EM&gt;if-then&lt;/EM&gt; to choose the right format, but i was curious if there is another method as with&amp;nbsp;&lt;EM&gt;vvaluex&lt;/EM&gt; or &lt;EM&gt;symget&lt;/EM&gt; in a similar context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818066#M322900</guid>
      <dc:creator>Quodly</dc:creator>
      <dc:date>2022-06-14T13:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818071#M322903</link>
      <description>&lt;P&gt;This works:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data c1;
  z1 = "asdf";
  z2 = put(z1, x1_fmt.);
run;&lt;/LI-CODE&gt;
&lt;P&gt;Because the PUT statement works while the step is executing, processing data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This could not work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data c2;
  y = "1";
  z = "asdf";
  format z function(cats("x", y, "_fmt")); /* is there such a function? */
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the FORMAT statement works while the data step is compiling, before the step has read in any data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I'm confused by your clarification.&amp;nbsp; If you're not trying to assign a format to a variable, but you just want to use a format that already exists, you could do that with the PUTC function and use the VFORMAT function to look up the format.&amp;nbsp; That will work because they work at execution time.&amp;nbsp; Do you want something like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;  
  value $ z1_fmt 
    'asdf'='hi' 
    'jkl' ='mom' 
  ;
run ;

data c1 ;
  z1='asdf' ;
  format z1 $z1_fmt. ;
  z2='jkl' ;
  z3=putc(z2,vformat(z1)) ;
run ;

proc print ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:30:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818071#M322903</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-06-14T13:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818092#M322914</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283289"&gt;@Quodly&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thank you for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;my example is misleading for my actual intention: i want to use formats for table lookups. for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
  z1 = "asdf";
  z2 = put(z1, x1_fmt.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for that, i don't need to change the metadata of a data set. the format which i want to use for the table lookup depends on another variable (variable &lt;EM&gt;y&lt;/EM&gt; in my example). of course, i can just use&amp;nbsp;&lt;EM&gt;if-then&lt;/EM&gt; to choose the right format, but i was curious if there is another method as with&amp;nbsp;&lt;EM&gt;vvaluex&lt;/EM&gt; or &lt;EM&gt;symget&lt;/EM&gt; in a similar context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You seem to have left out the variable the value of 1 in this example.&amp;nbsp; If you have that variable then just use the PUTC() function.&amp;nbsp; If Z1 was a numeric variable you would use the PUTN() function.&amp;nbsp; &amp;nbsp;Or if you wanted Z2 to be numeric and Z1 was character then use the INPUTN() function.&amp;nbsp; All four of those functions allow the format to passed as a string instead of as part of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Also character variables need character formats.&amp;nbsp; The names of character formats start with a $.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  z2 = putc(z1, cats('$x',y,'_fmt.'));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jun 2022 14:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818092#M322914</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-14T14:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818117#M322928</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283289"&gt;@Quodly&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thank you for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;my example is misleading for my actual intention: i want to use formats for table lookups. for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c1;
  z1 = "asdf";
  z2 = put(z1, x1_fmt.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for that, i don't need to change the metadata of a data set. the format which i want to use for the table lookup depends on another variable (variable &lt;EM&gt;y&lt;/EM&gt; in my example). of course, i can just use&amp;nbsp;&lt;EM&gt;if-then&lt;/EM&gt; to choose the right format, but i was curious if there is another method as with&amp;nbsp;&lt;EM&gt;vvaluex&lt;/EM&gt; or &lt;EM&gt;symget&lt;/EM&gt; in a similar context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;Then you can use PUTC/PUTN&lt;/STRONG&gt; where you can generate the name via a string and pass that to the PUTC/PUTN function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 15:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818117#M322928</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-14T15:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818163#M322946</link>
      <description>&lt;P&gt;Great, that's what I've been looking for! Thank you all - I should have stated my question more clearly at the beginning.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 17:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818163#M322946</guid>
      <dc:creator>Quodly</dc:creator>
      <dc:date>2022-06-14T17:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: get format whose name contains the value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818165#M322947</link>
      <description>And thank you for the reminder that character formats start with a dollar sign. I'm always a bit confused by value/invalue, put/input; and now that I've learned of putc/putn...</description>
      <pubDate>Tue, 14 Jun 2022 17:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/get-format-whose-name-contains-the-value-of-a-variable/m-p/818165#M322947</guid>
      <dc:creator>Quodly</dc:creator>
      <dc:date>2022-06-14T17:30:37Z</dc:date>
    </item>
  </channel>
</rss>

