<?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: Get Percent of Total from Long Dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727223#M226129</link>
    <description>&lt;P&gt;I would recommend looking into PROC FREQ and the OUTPCT option on the TABLES statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for a way to get the percent of total from the total volume with a long dataset.&lt;/P&gt;
&lt;P&gt;My dataset example is as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Age Group	Gender	Month	Metric	Item	Total	WANT
0-13	FEMALE	Feb-19	NEW PURCHASES	TELEVISION	0	#DIV/0!
0-13	FEMALE	Feb-19	TOTAL PURCHASES	TELEVISION	0	
0-13	FEMALE	Feb-19	NEW PURCHASES	FOOD	5	38.5%
0-13	FEMALE	Feb-19	TOTAL PURCHASES	FOOD	13	
0-13	MALE	Feb-19	NEW PURCHASES	TELEVISION	1	50.0%
0-13	MALE	Feb-19	TOTAL PURCHASES	TELEVISION	2	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have a long dataset where one row has the new purchases and the second row has total purchases for any given month. I want to get the percent of total for each item by age group, gender, item, and month that are new;&amp;nbsp; the WANT column. Would this be accomplished using multiple proc sql or is there an easier way to do this using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Mar 2021 19:36:59 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-03-17T19:36:59Z</dc:date>
    <item>
      <title>Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727221#M226127</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for a way to get the percent of total from the total volume with a long dataset.&lt;/P&gt;
&lt;P&gt;My dataset example is as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Age Group	Gender	Month	Item	Count	WANT
0-13	FEMALE	Feb_2019_new	TELEVISION	0	#DIV/0!
0-13	FEMALE	Feb_2019_total	TELEVISION	0	
0-13	FEMALE	Feb_2019_new	FOOD	5	38.5%
0-13	FEMALE	Feb_2019_total	FOOD	13	
0-13	MALE	Feb_2019_new	TELEVISION	1	50.0%
0-13	MALE	Feb_2019_total	TELEVISION	2	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have a long dataset where one row has the new purchases and the second row has total purchases for any given month. I want to get the percent of total for each item by age group, gender, item, and monthly that are new;&amp;nbsp; the WANT column. Would this be accomplished using multiple proc sql or is there an easier way to do this using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 20:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727221#M226127</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-03-17T20:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727223#M226129</link>
      <description>&lt;P&gt;I would recommend looking into PROC FREQ and the OUTPCT option on the TABLES statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for a way to get the percent of total from the total volume with a long dataset.&lt;/P&gt;
&lt;P&gt;My dataset example is as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Age Group	Gender	Month	Metric	Item	Total	WANT
0-13	FEMALE	Feb-19	NEW PURCHASES	TELEVISION	0	#DIV/0!
0-13	FEMALE	Feb-19	TOTAL PURCHASES	TELEVISION	0	
0-13	FEMALE	Feb-19	NEW PURCHASES	FOOD	5	38.5%
0-13	FEMALE	Feb-19	TOTAL PURCHASES	FOOD	13	
0-13	MALE	Feb-19	NEW PURCHASES	TELEVISION	1	50.0%
0-13	MALE	Feb-19	TOTAL PURCHASES	TELEVISION	2	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have a long dataset where one row has the new purchases and the second row has total purchases for any given month. I want to get the percent of total for each item by age group, gender, item, and month that are new;&amp;nbsp; the WANT column. Would this be accomplished using multiple proc sql or is there an easier way to do this using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 19:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727223#M226129</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-17T19:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727232#M226137</link>
      <description>&lt;P&gt;Sorry, I modified the example case slightly. Wouldn't that not work since I have these under the same variable therefore if I did a frequency table, my output would just sum them together when I'd really only want New row/Total row?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 20:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727232#M226137</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-03-17T20:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727253#M226151</link>
      <description>You're correct but your question (and data) are awkwardly stated - your tables isn't aligned either. &lt;BR /&gt;&lt;BR /&gt;If you can guarantee that you will always have New then Total you can look at the DIF and LAG() functions. &lt;BR /&gt;MOD() can be used to tell you if a value is odd/even. &lt;BR /&gt;_N_ will give you the row number. &lt;BR /&gt;or BY group processing. &lt;BR /&gt;&lt;BR /&gt;From what you've shown this would work but this may not scale:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by item;&lt;BR /&gt;want=lag(total)/total;&lt;BR /&gt;if first.item then call missing(want);&lt;BR /&gt;run;</description>
      <pubDate>Wed, 17 Mar 2021 21:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727253#M226151</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-17T21:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727270#M226162</link>
      <description>&lt;P&gt;My apologies. Let me see if I'm able to state my question a little better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A subset of my long dataset looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length name $20;
	infile datalines dsd missover delimiter=",";
	input age_grp $ gender $ name $ item $ count;
datalines;
0-13,FEMALE,FEB_2019_NEW                ,FOOD,12
0-13,FEMALE,FEB_2019_TOTAL              ,FOOD,23
0-13,FEMALE,FEB_2019_NEW                ,GAS,45
0-13,FEMALE,FEB_2019_TOTAL              ,GAS,76
0-13,FEMALE,FEB_2019_NEW                ,TELE,4
0-13,FEMALE,FEB_2019_TOTAL              ,TELE,5
0-13,MALE,FEB_2019_NEW                  ,TELE,3
0-13,MALE,FEB_2019_TOTAL                ,TELE,8
0-13,FEMALE,MAR_2019_NEW                ,TELE,0
0-13,FEMALE,MAR_2019_TOTAL              ,TELE,0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to turn the dataset into something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length name $20;
	infile datalines dsd missover delimiter=",";
	input age_grp $ gender $ name $ item $ count want;
datalines;
0-13,FEMALE,FEB_2019_NEW                ,FOOD,12,.5219
0-13,FEMALE,FEB_2019_TOTAL              ,FOOD,23,
0-13,FEMALE,FEB_2019_NEW                ,GAS,45,.592
0-13,FEMALE,FEB_2019_TOTAL              ,GAS,76, 
0-13,FEMALE,FEB_2019_NEW                ,TELE,4,.80
0-13,FEMALE,FEB_2019_TOTAL              ,TELE,5,
0-13,MALE,FEB_2019_NEW                  ,TELE,3,.375
0-13,MALE,FEB_2019_TOTAL                ,TELE,8, 
0-13,FEMALE,MAR_2019_NEW                ,TELE,0,.
0-13,FEMALE,MAR_2019_TOTAL              ,TELE,0, 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My BY variables include: Age_grp, Gender, Name, and Item.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, for each month, I want to report the percent of NEW/TOTAL for each age group, gender, and item combination. So, for food purchases among female 0-13 in February 2019, it was 23 total purchases but the percent of those that were NEW were 52.19% (.5219).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think a lag function might work but wasn't entirely sure how to do the grouping using your example. As you mentioned, I can guarantee that each row will alternate between New then Total.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then to take it one step further, I wanted to concatenate the values for TOTAL and NEW to produce a value for each age group, gender, and item combination to read out the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TOTAL value (NEW pct). So, in the example above, it would be 23(52.2%).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 22:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727270#M226162</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-03-17T22:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727283#M226169</link>
      <description>Sort your data so that total is before new and then use the method I included initially, reversing the calculation obviously. SAS doesn't do LEAD well, but LAG is relatively easy. Then resort for you display.</description>
      <pubDate>Thu, 18 Mar 2021 01:02:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727283#M226169</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-18T01:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727287#M226171</link>
      <description>&lt;P&gt;So, I did what you suggested:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
        set monthly_results;
        by  item age_new gender;
        want=total/lag(total);
        if first.item then call missing(want);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the value for new/total in the NEW row; however, I get a value in the TOTAL row instead of a MISSING value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally, if I then wanted to concatenate the TOTAL (want) together, how would I go about this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would I just be better suited to do this using proc sql or transpose the data?&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 01:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727287#M226171</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-03-18T01:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727309#M226187</link>
      <description>&lt;P&gt;If your data reliably has a single month value followed by a total value, then you can use a merge of HAVE with itself offset by one record:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length name $20;
	infile datalines dsd missover delimiter=",";
	input age_grp $ gender $ name $ item $ count;
datalines;
0-13,FEMALE,FEB_2019_NEW                ,FOOD,12
0-13,FEMALE,FEB_2019_TOTAL              ,FOOD,23
0-13,FEMALE,FEB_2019_NEW                ,GAS,45
0-13,FEMALE,FEB_2019_TOTAL              ,GAS,76
0-13,FEMALE,FEB_2019_NEW                ,TELE,4
0-13,FEMALE,FEB_2019_TOTAL              ,TELE,5
0-13,MALE,FEB_2019_NEW                  ,TELE,3
0-13,MALE,FEB_2019_TOTAL                ,TELE,8
0-13,FEMALE,MAR_2019_NEW                ,TELE,0
0-13,FEMALE,MAR_2019_TOTAL              ,TELE,0
;
run;

data want (drop=_:);
  merge have 
        have (firstobs=2 keep=count rename=(count=_total_count));
  if _total_count^=0 and scan(name,-1,'_')='NEW' then newvar=count/_total_count;
run;&lt;BR /&gt;&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;Now if once in a while there is no total following the new, then the self-merge with offset still works, but you have check that the name variables correspond:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  merge have 
        have (firstobs=2 keep=name count rename=(name=_total_name count=_total_count));
  if scan(name,-1,'_')='NEW' and _total_name=transtrn(name,'_NEW','_TOTAL') and _total_count^=0 
    then newvar=count/_total_count;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 06:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727309#M226187</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-18T06:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get Percent of Total from Long Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727457#M226257</link>
      <description>&lt;P&gt;This appears to work exactly how I described.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 16:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-Percent-of-Total-from-Long-Dataset/m-p/727457#M226257</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-03-18T16:05:35Z</dc:date>
    </item>
  </channel>
</rss>

