<?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 Creating a new variable from existing variables and adding it into a proc tabulate in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915141#M360609</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a completion variable (if PinT =1 then Completion=cost/subtotal) then I want to add this variable into my proc tabulate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93430i60B90B798C28AD89/image-dimensions/400x145?v=v2" width="400" height="145" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is what I want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 392px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93429i28A984ECA80EB0EC/image-dimensions/392x190?v=v2" width="392" height="190" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is the sample data I am working with:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Pint	Cost State$ Year Clients ;
datalines;

1	4789	WY	2023 	3
0	1589	WI	2023	8
0	2569	WY	2024	15	
0	5896	WI	2020	1
1	2000	WY	2024	8
1	4000	WI	2021	9
0	400		WI	2020	15
0	2000	WY	2021	12
0	2000	WY	2021	10	
0	2		WI	2020	7
0	14		WY	2020	2
0	3000	WI	2022	8
0	1000	WY	2022	7
0	43		WI	2021	4	
0	800		WY	2020	2
0	20		WI	2021	2
0	1000	WY	2021	1	
0	500		WI	2020	7
0	42		WY	2020	9
0	6000	WY	2023	7
1	5000	WI	2023	5	
1	258 	WI	2024	7
0	2569	WI	2024	9
1	500		WI	2020	2
1	42		WI	2020	2	
1	256		WY	2020	8
1	478		WY	2022	2
1	879		WY	2021	8
1	300		WI	2022	2
;;;
run;

Proc tabulate data=have format=dollar12. out=want1;
class State Year Pint ;
var cost ;
table  State='State'*(Pint all="Subtotal") ,(cost='$'* Year='Year')*sum=""/printmiss ;

run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Feb 2024 20:33:02 GMT</pubDate>
    <dc:creator>Whitlea</dc:creator>
    <dc:date>2024-02-08T20:33:02Z</dc:date>
    <item>
      <title>Creating a new variable from existing variables and adding it into a proc tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915141#M360609</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a completion variable (if PinT =1 then Completion=cost/subtotal) then I want to add this variable into my proc tabulate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93430i60B90B798C28AD89/image-dimensions/400x145?v=v2" width="400" height="145" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is what I want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 392px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93429i28A984ECA80EB0EC/image-dimensions/392x190?v=v2" width="392" height="190" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is the sample data I am working with:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Pint	Cost State$ Year Clients ;
datalines;

1	4789	WY	2023 	3
0	1589	WI	2023	8
0	2569	WY	2024	15	
0	5896	WI	2020	1
1	2000	WY	2024	8
1	4000	WI	2021	9
0	400		WI	2020	15
0	2000	WY	2021	12
0	2000	WY	2021	10	
0	2		WI	2020	7
0	14		WY	2020	2
0	3000	WI	2022	8
0	1000	WY	2022	7
0	43		WI	2021	4	
0	800		WY	2020	2
0	20		WI	2021	2
0	1000	WY	2021	1	
0	500		WI	2020	7
0	42		WY	2020	9
0	6000	WY	2023	7
1	5000	WI	2023	5	
1	258 	WI	2024	7
0	2569	WI	2024	9
1	500		WI	2020	2
1	42		WI	2020	2	
1	256		WY	2020	8
1	478		WY	2022	2
1	879		WY	2021	8
1	300		WI	2022	2
;;;
run;

Proc tabulate data=have format=dollar12. out=want1;
class State Year Pint ;
var cost ;
table  State='State'*(Pint all="Subtotal") ,(cost='$'* Year='Year')*sum=""/printmiss ;

run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 20:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915141#M360609</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-02-08T20:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from existing variables and adding it into a proc tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915194#M360625</link>
      <description>&lt;P&gt;That is not easy for PROC TABULATE.&lt;/P&gt;
&lt;P&gt;But easy for PROC SQL and PROC REPORT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Pint	Cost State$ Year Clients ;
datalines;

1	4789	WY	2023 	3
0	1589	WI	2023	8
0	2569	WY	2024	15	
0	5896	WI	2020	1
1	2000	WY	2024	8
1	4000	WI	2021	9
0	400		WI	2020	15
0	2000	WY	2021	12
0	2000	WY	2021	10	
0	2		WI	2020	7
0	14		WY	2020	2
0	3000	WI	2022	8
0	1000	WY	2022	7
0	43		WI	2021	4	
0	800		WY	2020	2
0	20		WI	2021	2
0	1000	WY	2021	1	
0	500		WI	2020	7
0	42		WY	2020	9
0	6000	WY	2023	7
1	5000	WI	2023	5	
1	258 	WI	2024	7
0	2569	WI	2024	9
1	500		WI	2020	2
1	42		WI	2020	2	
1	256		WY	2020	8
1	478		WY	2022	2
1	879		WY	2021	8
1	300		WI	2022	2
;;;
run;

proc sql;
create table want as
select state,'  '||put(pint,best8. -l) as pint length=80,year,put(sum(cost),dollar32. -l) as cost length=80
 from have
  group by state,pint,year
union
select state,' Subtotal',year,put(sum(cost),dollar32. -l) as cost
 from have
  group by state,year
union
select state,'Completion',year,put((select sum(cost) from have where state=a.state and year=a.year and pint=1)/sum(cost),percent8.2 -l) as cost
 from have as a
  group by state,year
;
quit;
proc report data=want nowd;
column state pint cost,year;
define state/group;
define pint/group;
define year/across;
define cost/group '$';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1707454090917.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93446iBF122168A593C97B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1707454090917.png" alt="Ksharp_0-1707454090917.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2024 04:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915194#M360625</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-09T04:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from existing variables and adding it into a proc tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915211#M360631</link>
      <description>Thank you!!</description>
      <pubDate>Fri, 09 Feb 2024 10:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915211#M360631</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-02-09T10:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from existing variables and adding it into a proc tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915232#M360639</link>
      <description>&lt;P&gt;When I attempt to add client count into the table I get the following error:&lt;/P&gt;&lt;P&gt;WARNING: A table has been extended with null columns to perform the UNION set operation. Do you know why I am getting this error?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
create table want as
select state,'  '||put(pint,best8. -l) as pint length=80,year,put(sum(cost),dollar32. -l) as cost length=80,put(sum(Clients),comma10. -l) as Clients length=80
 from have
  group by state,pint,year
union
select state,' Subtotal',year,put(sum(cost),dollar32. -l) as cost
 from have
  group by state,year
union
select state,' Subtotal',year,put(sum(Clients),comma10. -l) as Clients
 from have
  group by state,year
union
select state,'Completion Factor',year,put((select sum(cost) from have where state=a.state and year=a.year and pint=1)/sum(cost),percent8.2 -l) as cost
 from have as a
  group by state,year
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Feb 2024 13:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915232#M360639</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-02-09T13:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable from existing variables and adding it into a proc tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915371#M360675</link>
      <description>&lt;P&gt;So what kind of report would you like to see ? This one ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Pint	Cost State$ Year Clients ;
datalines;

1	4789	WY	2023 	3
0	1589	WI	2023	8
0	2569	WY	2024	15	
0	5896	WI	2020	1
1	2000	WY	2024	8
1	4000	WI	2021	9
0	400		WI	2020	15
0	2000	WY	2021	12
0	2000	WY	2021	10	
0	2		WI	2020	7
0	14		WY	2020	2
0	3000	WI	2022	8
0	1000	WY	2022	7
0	43		WI	2021	4	
0	800		WY	2020	2
0	20		WI	2021	2
0	1000	WY	2021	1	
0	500		WI	2020	7
0	42		WY	2020	9
0	6000	WY	2023	7
1	5000	WI	2023	5	
1	258 	WI	2024	7
0	2569	WI	2024	9
1	500		WI	2020	2
1	42		WI	2020	2	
1	256		WY	2020	8
1	478		WY	2022	2
1	879		WY	2021	8
1	300		WI	2022	2
;;;
run;

proc sql;
create table want as
select state,'  '||put(pint,best8. -l) as pint length=80,year,put(sum(cost),dollar32. -l) as cost length=80
,' Cost' as label length=80
 from have
  group by state,pint,year
union
select state,' Subtotal',year,put(sum(cost),dollar32. -l) as cost
,' Cost' as label length=80
 from have
  group by state,year
union
select state,'Completion',year,
put((select sum(cost) from have where state=a.state and year=a.year and pint=1)/sum(cost),percent8.2 -l) as cost
,' Cost' as label length=80
 from have as a
  group by state,year


union all

select state,'  '||put(pint,best8. -l) as pint length=80,year,put(sum(Clients),comma10. -l) as Clients
,'Count' as label length=80
 from have
  group by state,pint,year
union
select state,' Subtotal',year,put(sum(Clients),comma10. -l) as Clients
,'Count' as label length=80
 from have
  group by state,year
union
select state,'Completion',year,
put((select sum(Clients) from have where state=a.state and year=a.year and pint=1)/sum(Clients),percent8.2 -l) as Clients
,'Count' as label length=80
 from have as a
  group by state,year
;
quit;
proc report data=want nowd;
column state pint cost,year,label;
define state/group;
define pint/group;
define year/across;
define label/across '';
define cost/group '$';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1707528777838.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93512i2F311281BFAA2411/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1707528777838.png" alt="Ksharp_0-1707528777838.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 01:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-from-existing-variables-and-adding-it/m-p/915371#M360675</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-10T01:34:06Z</dc:date>
    </item>
  </channel>
</rss>

