<?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 calculate frequencies from multiple tables  and merge into one final table without key ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963694#M375394</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 200 tables like the sample table below and need to generate a final table with the frequency in percentage of records (7 in the sample table). The simple sample table is like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data temp;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC R
A IR W
C IC V
B ID V
;
RUN;&lt;/PRE&gt;
&lt;P&gt;What I want is like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CHL0320_0-1744118027379.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106016i11558F654E02A6EC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CHL0320_0-1744118027379.png" alt="CHL0320_0-1744118027379.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use proc freq to get the percentage, but I am struggling to put them all into one final big table with the Filename variable as the dataset name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance for your suggestion.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Apr 2025 13:19:29 GMT</pubDate>
    <dc:creator>CHL0320</dc:creator>
    <dc:date>2025-04-08T13:19:29Z</dc:date>
    <item>
      <title>calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963694#M375394</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 200 tables like the sample table below and need to generate a final table with the frequency in percentage of records (7 in the sample table). The simple sample table is like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data temp;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC R
A IR W
C IC V
B ID V
;
RUN;&lt;/PRE&gt;
&lt;P&gt;What I want is like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CHL0320_0-1744118027379.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106016i11558F654E02A6EC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CHL0320_0-1744118027379.png" alt="CHL0320_0-1744118027379.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use proc freq to get the percentage, but I am struggling to put them all into one final big table with the Filename variable as the dataset name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance for your suggestion.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 13:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963694#M375394</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2025-04-08T13:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963700#M375396</link>
      <description>&lt;P&gt;How about this example? I only use two data sets, temp1 and temp2, and in fact the data and percents are identical, but the point is to illustrate the method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp1;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC R
A IR W
C IC V
B ID V
;
RUN;

data temp2;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC R
A IR W
C IC V
B ID V
;
RUN;

data all;
    /* Note: if you really have 200 data sets, just use temp1-temp200 below */
    /* or whatever the real names are */
    set temp1-temp2 indsname=indsname;
    file=propcase(scan(indsname,2,'.'));
run;
proc freq data=all;
    by file;
    tables activity/out=act;
    tables location/out=loc;
    tables item/out=item;
run;
data all2;
    length characteristic activity location item $30;
    set act loc item;
    if not missing(activity) then activity=catx('~',"Activity",activity);
    if not missing(location) then location=catx('~','Location',location);
    if not missing(item) then item=catx('~','Item',item);
    characteristic=coalescec(activity,location,item);
run;

proc report data=all2 split='~';
    columns ("File~Name" file) ("Percent" percent),characteristic;
    define file/group ' ';
    define characteristic/across ' ';
    define percent/sum ' ' format=8.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1744119969583.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106018i69E8895BA6EB67C9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PaigeMiller_0-1744119969583.png" alt="PaigeMiller_0-1744119969583.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that it is always a good idea to make the table easily readable by your intended audience. This includes using capital letters and English words. Using per_ic, which is not an English word and is not capitalized,&amp;nbsp;&lt;SPAN&gt;is not as clear and readable as Percent Location IC which uses English words and has capitalized words. Note also File Name has capitalized file names and English words (filename is not really an English word). Furthermore, if R V W for Items have actual names or values, these should appear in the table as well. Activity should not be A B C but rather the actual English word names that A B C represent. And so on. Make the table readable!!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 14:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963700#M375396</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-04-08T14:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963712#M375398</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp1;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC V
A IR W
C IC V
B ID V
;
RUN;

data temp2;
input activity $ location $ item $;
datalines;
A IC V
A ID R
B IR W
C IC R
A IR W
A IC W
B ID V
;
RUN;

data temp3;
input activity $ location $ item $;
datalines;
A IC R
B ID R
B IR W
C IC R
A IR W
C IC V
B ID V
;
RUN;

proc sql noprint;
select compbl(memname || compress(' (in=ds_' || memname || ')')) into :tlist separated by ' '
from dictionary.tables where libname='WORK' and upcase(memname) like 'TEMP%';
quit;

%put confirm this looks right: &amp;amp;tlist;

data all (rename=(lastfn=fname));
set &amp;amp;tlist end=last;
length lastfn fname $32;
array T {*} ds_:;
do i=1 to dim(T);
	if T[i] then do;
		fname=substr(vname(T[i]),4);
		leave;
	end;
end;
lastfn=lag(fname);
length per_A per_B per_C per_IC per_ID per_IR per_V per_R per_W 8;
format per_: percent8.2;
array p1 {*} per_A per_B per_C;
array p2 {*} per_IC per_ID per_IR;
array p3 {*} per_V per_R per_W;
if fname^=lastfn then do;
	if _N_&amp;gt;1 then do;
		do i=1 to dim(p1);
			p1[i]=p1[i]/nrecs;
		end;
		do i=1 to dim(p2);
			p2[i]=p2[i]/nrecs;
		end;
		do i=1 to dim(p3);
			p3[i]=p3[i]/nrecs;
		end;
		output;
	end;
	call missing(of p1[*], of p2[*], of p3[*]);
	nrecs=0;
end;
nrecs+1;
do i=1 to dim(p1);
	p1[i]+(activity=scan(vname(p1[i]),2,'_'));
end;
do i=1 to dim(p2);
	p2[i]+(location=scan(vname(p2[i]),2,'_'));
end;
do i=1 to dim(p3);
	p3[i]+(item=scan(vname(p3[i]),2,'_'));
end;
if last then do;
	lastfn=fname;
	do i=1 to dim(p1);
		p1[i]=p1[i]/nrecs;
	end;
	do i=1 to dim(p2);
		p2[i]=p2[i]/nrecs;
	end;
	do i=1 to dim(p3);
		p3[i]=p3[i]/nrecs;
	end;
	output;
end;
keep lastfn per_:;
run;

proc print data=all heading=v width=min; run;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1744124960069.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106026i75CF61C0DDA2CA63/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1744124960069.png" alt="quickbluefish_0-1744124960069.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 15:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963712#M375398</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-04-08T15:09:26Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963714#M375400</link>
      <description>&lt;P&gt;Thank you. The values in the final table are the percentage of observation. The value is (the count of the original value in the column/total record number)*100.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 14:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963714#M375400</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2025-04-08T14:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963716#M375401</link>
      <description>ah, misread your original post - I edited the code above.</description>
      <pubDate>Tue, 08 Apr 2025 15:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963716#M375401</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-04-08T15:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963717#M375402</link>
      <description>&lt;P&gt;NRECS is a variable that is not defined before you use it in this statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;p1[i]=p1[i]/nrecs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original question said there are over 200 data sets, how do you up-scale this code to that case? My code handles this case easily, as the comment in my code indicates.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 15:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963717#M375402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-04-08T15:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963718#M375403</link>
      <description>good point - it should be in the LENGTH statement - the reason it's working is because of the IF _N_&amp;gt;1 THEN...  part.&lt;BR /&gt;&lt;BR /&gt;It's the SQL part that's dealing with any input datasets starting with TEMP.  I think it should work for any reasonable number (within the max allowed size of a macro var).</description>
      <pubDate>Tue, 08 Apr 2025 15:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963718#M375403</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-04-08T15:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963797#M375424</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thank you for the solution. It works very neatly.&amp;nbsp; There is an error to request that&amp;nbsp; I need to do proc sort by file before proceeding to the step of proc freq. The order in the final table is like temp1, temp10.........temp2, temp 20....... Any suggestions to make the order like temp 1, temp2, temp3......? Thank you so much.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Apr 2025 22:36:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963797#M375424</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2025-04-08T22:36:53Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963805#M375429</link>
      <description>&lt;P&gt;Try this one :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=all out=want &lt;STRONG&gt;sortseq=linguistic(numeric_collation=on)&lt;/STRONG&gt;;
&lt;STRONG&gt;by file;&lt;/STRONG&gt;
run;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Apr 2025 01:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963805#M375429</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-04-09T01:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963831#M375438</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/108652"&gt;@CHL0320&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thank you for the solution. It works very neatly.&amp;nbsp; There is an error to request that&amp;nbsp; I need to do proc sort by file before proceeding to the step of proc freq. The order in the final table is like temp1, temp10.........temp2, temp 20....... Any suggestions to make the order like temp 1, temp2, temp3......? Thank you so much.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This raises another question. Is the intended audience for your table output familiar with what is in data set temp1 and temp2 and ... and temp200? Maybe, but I think this is unlikely. Meaningful descriptions of these data sets ought to be used in the table instead of file1 file2 etc. (The goal isn't to get the programming to work; the goal is to create a table that people will understand, and programming is a part of the steps needed to obtain that table)&lt;/P&gt;</description>
      <pubDate>Wed, 09 Apr 2025 10:02:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963831#M375438</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-04-09T10:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies from multiple tables  and merge into one final table without key ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963864#M375460</link>
      <description>I have somehow never seen the INDSNAME option before - very useful!</description>
      <pubDate>Wed, 09 Apr 2025 11:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies-from-multiple-tables-and-merge-into-one/m-p/963864#M375460</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-04-09T11:58:31Z</dc:date>
    </item>
  </channel>
</rss>

