<?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: Proc Format cntlin numeric informat in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935543#M367830</link>
    <description>You may need an iterative solution:  fix the earliest problem, then see what problems remain.&lt;BR /&gt;Since you report no errors, we won't try to fix any errors.  Instead, sort the data set fmt by FMTNAME, before feeding it in to PROC FORMAT.&lt;BR /&gt;Last I checked, a new value for FMTNAME triggers replacement of that format, not the addition to previously defined values.</description>
    <pubDate>Fri, 12 Jul 2024 05:54:33 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2024-07-12T05:54:33Z</dc:date>
    <item>
      <title>Proc Format cntlin numeric informat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935542#M367829</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have 2 datasets as below: 'insects' and 'test1'.&lt;/P&gt;
&lt;P&gt;Data test1 has thousands of rows with many columns including binary 1/0 ins_ant, ins_bee. ins_beetle.&lt;/P&gt;
&lt;P&gt;I want to calculate new columns 'total_weight' and 'total_id' based on the values in Data insects (sum of the corresponding "weight"s and "id"s.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using a proc format to do this, my code is below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used type = 'I' because my input is a string (insect name) and outputs are numbers, and that's the informat type to use to the documentation here -&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0owa4ftikc2ekn1q0rmpulg86cx.htm" target="_blank" rel="noopener"&gt;SAS Help Center: Results: PROC FORMAT&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But my final output dataset test2 has null values for the total_weight and total_id columns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or please let me know if there's a simpler way to do this (without using proc format, but also without hardcoding anything like names of insects/weights/ids/number of insects etc.)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code:&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;
&lt;LI-CODE lang="sas"&gt;
data insects;
length insect $100.;
	insect='ant';
	weight = 10;
	id0 = 1;
	output;

	insect='beetle';
	weight = 20;
	id0 = 2;
	output;

	insect='bee';
	weight = 30;
	id0 = 3;
	output;
run;

data fmt;
	set insects;
	length start $100;
	start = upcase(strip(insect));

	retain type 'I';

	fmtname = 'weight';
	label = weight;
	output fmt;

	fmtname = 'id';
	label = id0;
	output fmt;

	keep start fmtname label type;
run;

proc format cntlin=fmt;
run;

data test1;
	
	patient = 4772;
	ins_ant = 1;
	ins_bee = 1;
	ins_beetle = 0;
	output;

	patient = 6792;
	ins_ant = 0;
	ins_bee = 1;
	ins_beetle = 1;
	output;
run;

data test2;
	set test1;
	total_weight = 0;
	total_id0 = 0;

	array ins_all {*} ins_: ; 
	
	do i_local = 1 to dim(ins_all);
		total_weight = total_weight + ( ins_all[i_local] * input(upcase(strip(scan(vname(ins_all[i_local]),2,'_'))),weight.) );
		total_id0 = total_id0 + ( ins_all[i_local] * input(upcase(strip(scan(vname(ins_all[i_local]),2,'_'))),id.) );
	end;

	drop i_local;
run;
&lt;/LI-CODE&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2024 05:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935542#M367829</guid>
      <dc:creator>sc5</dc:creator>
      <dc:date>2024-07-12T05:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Format cntlin numeric informat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935543#M367830</link>
      <description>You may need an iterative solution:  fix the earliest problem, then see what problems remain.&lt;BR /&gt;Since you report no errors, we won't try to fix any errors.  Instead, sort the data set fmt by FMTNAME, before feeding it in to PROC FORMAT.&lt;BR /&gt;Last I checked, a new value for FMTNAME triggers replacement of that format, not the addition to previously defined values.</description>
      <pubDate>Fri, 12 Jul 2024 05:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935543#M367830</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-07-12T05:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Format cntlin numeric informat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935547#M367831</link>
      <description>Sorting worked, thank you! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 12 Jul 2024 05:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935547#M367831</guid>
      <dc:creator>sc5</dc:creator>
      <dc:date>2024-07-12T05:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Format cntlin numeric informat</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935584#M367837</link>
      <description>&lt;P&gt;If you have SAS/STAT licensed (why are you running SAS without it?) you can use PROC SCORE to do the work for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take your INSECT dataset and transpose it and add a _TYPE_ variable with constant value of 'SCORE'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data insects;
  _type_='SCORE';
  input insect :$32. weight id0 ;
cards;
ant    10 1
beetle 20 2
bee    30 3
;

proc transpose data=insects out=insect_score;
  by _type_;
  id insect;
  var weight id0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use it to score your test data. Make sure the variable names match the values of _NAME_ in the scoring dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  input patient ant bee beetle ;
cards;
4772 1 1 0
6792 0 1 1
;

proc score data=test1 score=insect_score out=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    patient    ant    bee    beetle    weight    id0

 1       4772      1      1        0        40       4
 2       6792      0      1        1        50       5

&lt;/PRE&gt;
&lt;P&gt;PS Notice how much easier it is to share data using in-line data an INPUT statement instead of multiple assignment statements and OUTPUT statement.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2024 14:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Format-cntlin-numeric-informat/m-p/935584#M367837</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-12T14:32:05Z</dc:date>
    </item>
  </channel>
</rss>

