<?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 Formats in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42414#M11038</link>
    <description>What result do you expect (and want) to see?  Suggest you share what code you are executing to be a bit more specific here.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Tue, 19 Jan 2010 14:17:08 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2010-01-19T14:17:08Z</dc:date>
    <item>
      <title>Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42413#M11037</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
Lets say I have the variable "sifmt" which has the format values 4.1 , 5.1, 3.2, 7.2 etc for each lab test as below:&lt;BR /&gt;
&lt;BR /&gt;
Test        Sifmt&lt;BR /&gt;
Albumin   4.1&lt;BR /&gt;
Protein    5.1&lt;BR /&gt;
WBC      6.2&lt;BR /&gt;
........&lt;BR /&gt;
.....&lt;BR /&gt;
etc.&lt;BR /&gt;
&lt;BR /&gt;
I am doing calculation with the format as below: &lt;BR /&gt;
put(result*factor, 4.1) for Albumin and put(result*factor, 5.1) for Protien, etc. I don't want to use repetitive if conditions becuase these format values may change.&lt;BR /&gt;
&lt;BR /&gt;
Any help would be appreciated !!!&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
/Sukanya</description>
      <pubDate>Tue, 19 Jan 2010 06:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42413#M11037</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-19T06:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42414#M11038</link>
      <description>What result do you expect (and want) to see?  Suggest you share what code you are executing to be a bit more specific here.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 19 Jan 2010 14:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42414#M11038</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-01-19T14:17:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42415#M11039</link>
      <description>I suggest that you create a macro that will dynamically generate the code (if-then/select/case), based on the values in your look-up table (with Test and Sifmt columns).&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Tue, 19 Jan 2010 14:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42415#M11039</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2010-01-19T14:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42416#M11040</link>
      <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
Depending on the number of tests/formats, you could automate the format transformation by loading everything into one or two macro lists. &lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select TEST, SIFMT into :L_TESTS  separated by ' ' , :L_TESTS_FMT separated by ' ' from TESTSLIST;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%put L_TESTS=&amp;amp;L_TESTS;&lt;BR /&gt;
%put L_TESTS_FMT=&amp;amp;L_TESTS_FMT;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
This will populate two macro vars (L_TEST and L_TESTS_FMT) each other with a list of elements (one with the test name, and the other with the corresponding format) separated by blanks.&lt;BR /&gt;
&lt;BR /&gt;
Then you need to make a match when processing data with the corresponding test name to obtain its index position on the list. Then, the second list (the one with the formats) will hold at the same position the adequate format. Just pass it to the putn function, and its a done deal.&lt;BR /&gt;
&lt;BR /&gt;
Something like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data OUTDATA;&lt;BR /&gt;
set INDATA;&lt;BR /&gt;
&lt;BR /&gt;
* cycle through the list, match the test name and retrieve the element index;&lt;BR /&gt;
IDX=0;&lt;BR /&gt;
do until(scan("&amp;amp;L_TESTS",IDX) eq TEST or scan("&amp;amp;L_TESTS",IDX) eq '');&lt;BR /&gt;
IDX+1;&lt;BR /&gt;
end;&lt;BR /&gt;
if scan("(&amp;amp;L_TESTS",IDX) eq '' then IDX=0; * test name not matched, IDX=0;&lt;BR /&gt;
&lt;BR /&gt;
* apply automatically the correct format; &lt;BR /&gt;
FINAL=putn(result*factor,scan(symget('L_TESTS_FMT'),IDX,' '));&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Completely automatic and not quite complex as it may seem.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Tue, 19 Jan 2010 16:46:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42416#M11040</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-19T16:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42417#M11041</link>
      <description>Hi:&lt;BR /&gt;
  I don't believe you need fancy macro coding at all for this. The PUT function does not allow a variable or constant as the value of a format name. However, the PUTN function does. So you have to make a variable to hold the name of the format and then -use- that variable with the PUTN function, as shown in the program below.&lt;BR /&gt;
 &lt;BR /&gt;
  Basically, the PUT statement:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  applyfmt = put(test,$appfmt.);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                               &lt;BR /&gt;
does the lookup and then the PUTN statement -uses- APPLYFMT to create the new variable:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  newval = putn(result*factor,applyfmt);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
            &lt;BR /&gt;
The entire program and results are below. I wasn't sure about the values for result and factor, so I just made up some numbers. Remember that when you assign the format size, that you must account for all the characters, numbers and punctuation in the size. So, this number 111.1 would be a size of 5.1, meaning 5 total characters with a decimal point and 1 character after the decimal. If you needed 11111.1, then the format would be 7.1 -- 7 total characters with a decimal point and 1 character after the decimal point.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** Program;&lt;BR /&gt;
data testdata;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input Test $ result factor;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Albumin 11 2&lt;BR /&gt;
Protein 111 2&lt;BR /&gt;
WBC 111 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
proc format;&lt;BR /&gt;
  value $appfmt 'Albumin'= '4.1'&lt;BR /&gt;
               'Protein'= '5.1'&lt;BR /&gt;
               'WBC'= '6.2';&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
data domult;&lt;BR /&gt;
  length newval $12;&lt;BR /&gt;
  set testdata;&lt;BR /&gt;
              &lt;BR /&gt;
  ** One approach -- use cascading IF statements or macro variables; &lt;BR /&gt;
              &lt;BR /&gt;
  ** Alternate approach -- associate the numeric format with particular TEST;&lt;BR /&gt;
  ** Note -- use the PUTN function because it allows a variable to supply the name of a format;&lt;BR /&gt;
  applyfmt = put(test,$appfmt.);&lt;BR /&gt;
  newval = putn(result*factor,applyfmt);&lt;BR /&gt;
run;&lt;BR /&gt;
                                    &lt;BR /&gt;
options nodate nonumber;&lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=domult;&lt;BR /&gt;
title 'Using APPLYFMT to supply format value for PUTN';&lt;BR /&gt;
run;&lt;BR /&gt;
                            &lt;BR /&gt;
** Results;&lt;BR /&gt;
             Using APPLYFMT to supply format value for PUTN&lt;BR /&gt;
                                   &lt;BR /&gt;
        Obs    newval    Test       result    factor    applyfmt&lt;BR /&gt;
         1     22.0      Albumin       11        2        4.1&lt;BR /&gt;
         2     222.0     Protein      111        2        5.1&lt;BR /&gt;
         3     222.00    WBC          111        2        6.2&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 19 Jan 2010 17:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42417#M11041</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-01-19T17:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42418#M11042</link>
      <description>Cynthia, interesting approach.&lt;BR /&gt;
&lt;BR /&gt;
I would stick with this one (easier to understand), unless the test/format list is quite extensive. In that case you could code the lookup format through the contents of a table, but then, everything gets more complex also.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Tue, 19 Jan 2010 17:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42418#M11042</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-19T17:59:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Formats</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42419#M11043</link>
      <description>Thank you guys... It is working...&lt;BR /&gt;
&lt;BR /&gt;
/SUkanya</description>
      <pubDate>Mon, 25 Jan 2010 22:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dynamic-Formats/m-p/42419#M11043</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-25T22:25:39Z</dc:date>
    </item>
  </channel>
</rss>

