<?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: Dynamic apply format by making the fmtname from a value in the same dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335413#M75909</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* T1002580 Using putc or putn with format names in your data

Using putc or putn with format names in your data

This seems to work

inspired by
https://goo.gl/ktxY1O
https://communities.sas.com/t5/Base-SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335244

HAVE
====

proc format;
      value $ FMTA1X
      "A" = "this was discount $10"
      "B" = "this was discount $20"
      ;

      value $ FMTA2X
      "A" = "this was discount $300"
      "B" = "this was discount $400"
            ;
run;

Up to 40 obs WORK.HAVE total obs=5

Obs    PRODTYPE    DISCOUNT

 1        A1          B
 2        A2          A
 3        A2          A
 4        A2          B
 5        A1          B

WANT
====

Up to 40 obs WORK.DEMO_DESCRIPTION_HARDCODE total obs=5

Obs    PRODTYPE    DISCOUNT        DISCOUNT_DESC

 1        A1          B        this was discount $20   putc(discount,$fmta1x.)
 2        A2          A        this was discount $300
 3        A2          A        this was discount $300
 4        A2          B        this was discount $400
 5        A1          B        this was discount $20


SOLUTION
========

data have;
input Prodtype $ Discount $;
cards4;
A1 B
A2 A
A2 A
A2 B
A1 B
;;;;
run;quit;

data want;
  retain prodtype discount discount_desc;
  length discount_desc $44;
  set have;

  fmt=cats('$fmt',prodtype,'x');
  Discount_desc = putc(Discount,fmt);

run;quit;


Up to 40 obs WORK.WANT total obs=5

Obs    PRODTYPE    DISCOUNT        DISCOUNT_DESC           FMT

 1        A1          B        this was discount $20     $fmtA1x
 2        A2          A        this was discount $300    $fmtA2x
 3        A2          A        this was discount $300    $fmtA2x
 4        A2          B        this was discount $400    $fmtA2x
 5        A1          B        this was discount $20     $fmtA1x
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Feb 2017 21:08:34 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2017-02-23T21:08:34Z</dc:date>
    <item>
      <title>Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335244#M75855</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS9.4 on&amp;nbsp; a Windows10 PC environment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create a string column to contain a full description by using a format within a PUT statement.&amp;nbsp; I want the format name to be generated dynamically based on a value in a different column.&amp;nbsp;&amp;nbsp; This seems feasible but I can't quite nail it, example program attached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this problem on a couple of programs so all help will be gratefully received, thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 11:33:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335244#M75855</guid>
      <dc:creator>RB1Kenobi</dc:creator>
      <dc:date>2017-02-23T11:33:51Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335252#M75861</link>
      <description>&lt;P&gt;Not possible.&lt;/P&gt;
&lt;P&gt;The format is set when the data step is compiled, so formats need to be named literally.&lt;/P&gt;
&lt;P&gt;From the documentation of the put() function:&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="strong"&gt;&lt;EM&gt;&lt;SPAN class="strongEmph"&gt;format&lt;/SPAN&gt;&lt;/EM&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;A name="a000845532" target="_blank"&gt;&lt;/A&gt; contains the SAS format that you want applied to the value that is specified in the source. This argument must be the name of a format with a period and optional width and decimal specifications, &lt;STRONG&gt;not a character constant, variable, or expression&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;(Emphasis by me)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read the documentation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 11:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335252#M75861</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-23T11:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335254#M75863</link>
      <description>&lt;P&gt;Post your examples as text in a code window ({i} above post). &amp;nbsp;Is there a reason you want to try to automate it, you will need to have the list somewhere if so. &amp;nbsp;If its just a couple then what you have is fine. &amp;nbsp;To automate will take more code:&lt;/P&gt;
&lt;PRE&gt;data fmts;
  length fmt_cond fmt $200;
  input fmt_cond $ fmt $;
datalines;
A1 $fmt2
A2 $fmt1
;
run;

data _null_;
  set fmts  end=last;
  if _n_=1 then call execute('data want;  set have;');
  call execute('if variable="'||strip(fmt_cond)||'" then res=put(   ...);
  if last then call execute('run;');
run;&lt;/PRE&gt;
&lt;P&gt;Just as an example, however I would really question why this is necessary at all.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 11:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335254#M75863</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-23T11:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335277#M75867</link>
      <description>&lt;P&gt;PUTC/PUTN allow format names that are from a variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Borrowing code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data fmts;&lt;BR /&gt; length fmt_cond fmt $200;&lt;BR /&gt; input fmt_cond $ fmt $;&lt;BR /&gt;datalines;&lt;BR /&gt;A1 $fmt2&lt;BR /&gt;A2 $fmt1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;set fmts;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want_var = putc(fmt_cond, fmt);&lt;/P&gt;
&lt;P&gt;Run;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 14:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335277#M75867</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-23T14:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335305#M75875</link>
      <description>&lt;P&gt;I suspect you would also be well served by a hash lookup table, keyed on 2 vars: PRODTYPE and DISCOUNT.&amp;nbsp; Make a lookup dataset, with all the expected combinations of prodtype,discount, and discount_desc.&amp;nbsp; Then load it into a hash object to use in the data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
  input  prodtype :$2. discount :$1. @6 discount_desc $30.;
datalines;
A1 A this was discount $10
A1 B this was discount $20
A2 A this was discount $300
A2 B this was discount $400
run;

data demo_description_dynamic (drop=rc);
  set demodata;
  if _n_=1 then do;
    if 0 then set lookup;
    declare hash h (dataset:'lookup');
      h.definekey('prodtype','discount');
      h.definedata(all:'Y');
      h.definedone();
  end;
  rc=h.find();
  if rc^=0 then call missing(discount_desc);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the h.find() method is successful, two things happen:&amp;nbsp; RC=0 and discount_desc is retrieved from the desired item in hash object h.&amp;nbsp; If it's unsuccessful, RC ^=0 and &lt;EM&gt;&lt;STRONG&gt;discount_desc is unchanged from the last successful h.find()&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; That's why you see discount_desc set to missing when rc^=0.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 15:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335305#M75875</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-23T15:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335413#M75909</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* T1002580 Using putc or putn with format names in your data

Using putc or putn with format names in your data

This seems to work

inspired by
https://goo.gl/ktxY1O
https://communities.sas.com/t5/Base-SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335244

HAVE
====

proc format;
      value $ FMTA1X
      "A" = "this was discount $10"
      "B" = "this was discount $20"
      ;

      value $ FMTA2X
      "A" = "this was discount $300"
      "B" = "this was discount $400"
            ;
run;

Up to 40 obs WORK.HAVE total obs=5

Obs    PRODTYPE    DISCOUNT

 1        A1          B
 2        A2          A
 3        A2          A
 4        A2          B
 5        A1          B

WANT
====

Up to 40 obs WORK.DEMO_DESCRIPTION_HARDCODE total obs=5

Obs    PRODTYPE    DISCOUNT        DISCOUNT_DESC

 1        A1          B        this was discount $20   putc(discount,$fmta1x.)
 2        A2          A        this was discount $300
 3        A2          A        this was discount $300
 4        A2          B        this was discount $400
 5        A1          B        this was discount $20


SOLUTION
========

data have;
input Prodtype $ Discount $;
cards4;
A1 B
A2 A
A2 A
A2 B
A1 B
;;;;
run;quit;

data want;
  retain prodtype discount discount_desc;
  length discount_desc $44;
  set have;

  fmt=cats('$fmt',prodtype,'x');
  Discount_desc = putc(Discount,fmt);

run;quit;


Up to 40 obs WORK.WANT total obs=5

Obs    PRODTYPE    DISCOUNT        DISCOUNT_DESC           FMT

 1        A1          B        this was discount $20     $fmtA1x
 2        A2          A        this was discount $300    $fmtA2x
 3        A2          A        this was discount $300    $fmtA2x
 4        A2          B        this was discount $400    $fmtA2x
 5        A1          B        this was discount $20     $fmtA1x
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Feb 2017 21:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335413#M75909</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-23T21:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic apply format by making the fmtname from a value in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335555#M75968</link>
      <description>&lt;P&gt;Thank you all for your contributions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other way I was looking at the problem was to use create a function (Proc FCMP)&amp;nbsp;which would have been stored in one place and then reference across the different programs.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 10:45:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-apply-format-by-making-the-fmtname-from-a-value-in-the/m-p/335555#M75968</guid>
      <dc:creator>RB1Kenobi</dc:creator>
      <dc:date>2017-02-24T10:45:50Z</dc:date>
    </item>
  </channel>
</rss>

