<?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: Cost calculation in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607646#M17575</link>
    <description>&lt;P&gt;Please try the below code, a dynamic approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID May June July August September October Month$ _31_May_19 _30_Jun_19 _31_Jul_19 _31_Aug_19 _30_Sep_19 _31_Oct_19;
cards;
12345 3 2 2 2 2 2 May 0.57 0.63 0.89 2.32 1.63 0.4
12346 2 2 1 1 1 1 May 0.99 0.42 0.9 0.02 0.93 0.25
12347 1 1 1 1 1 1 xxx 0.64 0.93 0.95 0.21 0.75 0.52
12348 3 3 3 1 2 2 May 0.97 0.41 0.28 0.23 0.74 0.1
12349 1 2 2 2 3 2 September 0.83 0.23 0.76 0.56 0.97 0.43
12350 1 2 2 2 2 2 xxx 0.03 0.81 0.65 0.52 0.86 0.25
12351 2 2 2 1 1 1 May 0.73 0.29 0.18 0.60 0.35 0.58
12352 3 3 3 2 2 2 May 0.99 0.43 0.72 0.13 0.37 0.84
12353 3 2 2 2 2 2 May 0.67 0.20 0.36 0.11 0.86 0.51
12354 2 3 3 3 3 3 xxx 0.70 0.84 0.76 0.19 0.46 0.89
;

data want;
set have;
array dates(*) _31_May_19 _30_Jun_19 _31_Jul_19 _31_Aug_19 _30_Sep_19 _31_Oct_19;
do i = 1 to dim(dates);
if lowcase(substr(month,1,3))=lowcase(compress(vname(dates(i)),,'ka')) then cost=dates(i);
actual=cost-dates(i);
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Nov 2019 12:10:58 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2019-11-27T12:10:58Z</dc:date>
    <item>
      <title>Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607633#M17571</link>
      <description>&lt;P&gt;Hi,&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="samp.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34276i3349DAA1AC35ACC1/image-size/large?v=v2&amp;amp;px=999" role="button" title="samp.JPG" alt="samp.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;ID May June July August September October Month 31-May-19 30-Jun-19 31-Jul-19 31-Aug-19 30-Sep-19 31-Oct-19&lt;BR /&gt;12345 3 2 2 2 2 2 May 0.57 0.63 0.89 2.32 1.63 0.4&lt;BR /&gt;12346 2 2 1 1 1 1 May 0.99 0.42 0.9 0.02 0.93 0.25&lt;BR /&gt;12347 1 1 1 1 1 1 0.64 0.93 0.95 0.21 0.75 0.52&lt;BR /&gt;12348 3 3 3 1 2 2 May 0.97 0.41 0.28 0.23 0.74 0.1&lt;BR /&gt;12349 1 2 2 2 3 2 September 0.83 0.23 0.76 0.56 0.97 0.43&lt;BR /&gt;12350 1 2 2 2 2 2 0.03 0.81 0.65 0.52 0.86 0.25&lt;BR /&gt;12351 2 2 2 1 1 1 May 0.73 0.29 0.18 0.60 0.35 0.58&lt;BR /&gt;12352 3 3 3 2 2 2 May 0.99 0.43 0.72 0.13 0.37 0.84&lt;BR /&gt;12353 3 2 2 2 2 2 May 0.67 0.20 0.36 0.11 0.86 0.51&lt;BR /&gt;12354 2 3 3 3 3 3 0.70 0.84 0.76 0.19 0.46 0.89&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to add two new Columns COST and Actual&lt;BR /&gt;ID - Numeric Format&lt;BR /&gt;May, June, July, August, September, October, Month - Character Format&lt;BR /&gt;31-MAY-2019, 30-JUN-2019, 31-JUL-2019, 31-AUG-2019, 30-SEP-2019, 31-OCT-2019 - Numeric Format&lt;/P&gt;&lt;P&gt;1) Cost : Cost should only be populated when Month is available in MONTH Column&lt;BR /&gt;for e.g., if May Month in Month column then 31-MAY-2019 cost should come up in&lt;BR /&gt;COST column.&lt;/P&gt;&lt;P&gt;2) Actaul : ACTUAL should be calculated from COST and last month cost(here 31-OCT-2019)&lt;BR /&gt;will be last month&lt;BR /&gt;Actual Formula : ACTUAL = Cost - 31-OCT-2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need output like this:&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="samp2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34277iEF8593A679564520/image-size/large?v=v2&amp;amp;px=999" role="button" title="samp2.JPG" alt="samp2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 11:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607633#M17571</guid>
      <dc:creator>vnreddy</dc:creator>
      <dc:date>2019-11-27T11:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607644#M17573</link>
      <description>&lt;P&gt;The data you posted reminds me of someone else posting data in the same strange format. Weird.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First thing to do: fix the broken variable names like "31-May-19" - this name could be used, but would make reading and maintaining the code harder, so such names must be avoided. And as soon as you have the data as dataset, you should re-post the data as data-step using datalines statement, so that the community has something to actually work with. See &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; for details about posting data.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 12:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607644#M17573</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-11-27T12:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607645#M17574</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301412"&gt;@vnreddy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't agree more with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you can try the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines dlm=" " missover dsd;
	length month $10.;
	input ID May$ June$ July$ August$ September$ October$ Month$ "31-May-19"n "30-Jun-19"n "31-Jul-19"n "31-Aug-19"n "30-Sep-19"n "31-Oct-19"n;
	datalines;
12345 3 2 2 2 2 2 May 0.57 0.63 0.89 2.32 1.63 0.4
12346 2 2 1 1 1 1 May 0.99 0.42 0.9 0.02 0.93 0.25
12347 1 1 1 1 1 1  0.64 0.93 0.95 0.21 0.75 0.52
12348 3 3 3 1 2 2 May 0.97 0.41 0.28 0.23 0.74 0.1
12349 1 2 2 2 3 2 September 0.83 0.23 0.76 0.56 0.97 0.43
12350 1 2 2 2 2 2  0.03 0.81 0.65 0.52 0.86 0.25
12351 2 2 2 1 1 1 Jul 0.73 0.29 0.18 0.60 0.35 0.58
12352 3 3 3 2 2 2 May 0.99 0.43 0.72 0.13 0.37 0.84
12353 3 2 2 2 2 2 May 0.67 0.20 0.36 0.11 0.86 0.51
12354 2 3 3 3 3 3  0.70 0.84 0.76 0.19 0.46 0.89
;
run;

data want;
	set have;
	array mon(*) $ "31-May-19"n "30-Jun-19"n "31-Jul-19"n "31-Aug-19"n "30-Sep-19"n "31-Oct-19"n;
	do i=1 to dim(mon);
		if month ne "" and substr(vname(mon(i)),4,3) = substr(month,1,3) then do;
			cost = mon(i);
		leave;
		end;
	end;
	actual = Cost - "31-Oct-19"n;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Nov 2019 12:08:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607645#M17574</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-11-27T12:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607646#M17575</link>
      <description>&lt;P&gt;Please try the below code, a dynamic approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID May June July August September October Month$ _31_May_19 _30_Jun_19 _31_Jul_19 _31_Aug_19 _30_Sep_19 _31_Oct_19;
cards;
12345 3 2 2 2 2 2 May 0.57 0.63 0.89 2.32 1.63 0.4
12346 2 2 1 1 1 1 May 0.99 0.42 0.9 0.02 0.93 0.25
12347 1 1 1 1 1 1 xxx 0.64 0.93 0.95 0.21 0.75 0.52
12348 3 3 3 1 2 2 May 0.97 0.41 0.28 0.23 0.74 0.1
12349 1 2 2 2 3 2 September 0.83 0.23 0.76 0.56 0.97 0.43
12350 1 2 2 2 2 2 xxx 0.03 0.81 0.65 0.52 0.86 0.25
12351 2 2 2 1 1 1 May 0.73 0.29 0.18 0.60 0.35 0.58
12352 3 3 3 2 2 2 May 0.99 0.43 0.72 0.13 0.37 0.84
12353 3 2 2 2 2 2 May 0.67 0.20 0.36 0.11 0.86 0.51
12354 2 3 3 3 3 3 xxx 0.70 0.84 0.76 0.19 0.46 0.89
;

data want;
set have;
array dates(*) _31_May_19 _30_Jun_19 _31_Jul_19 _31_Aug_19 _30_Sep_19 _31_Oct_19;
do i = 1 to dim(dates);
if lowcase(substr(month,1,3))=lowcase(compress(vname(dates(i)),,'ka')) then cost=dates(i);
actual=cost-dates(i);
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 12:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607646#M17575</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-11-27T12:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607667#M17581</link>
      <description>&lt;P&gt;As mentioned by others, having variable names that look like calendar days or months is not usually a good programming practice.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But also, problems like this are often more easily handled in Excel, rather than SAS. In Excel, you don't have to change the variable names, and the programming relatively quick and straightforward, unlike in SAS. So this is a job for Excel, not SAS.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 13:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607667#M17581</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-27T13:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Cost calculation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607676#M17583</link>
      <description>&lt;P&gt;You suffer from bad data design.&lt;/P&gt;
&lt;P&gt;Look at this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input id $ mon_5 mon_6 mon_7 mon_8 mon_9 mon_10;
datalines;
12345 0.57 0.63 0.89 2.32 1.63 0.4
12346 0.99 0.42 0.9 0.02 0.93 0.25
12347 0.64 0.93 0.95 0.21 0.75 0.52
12348 0.97 0.41 0.28 0.23 0.74 0.1
12349 0.83 0.23 0.76 0.56 0.97 0.43
12350 0.03 0.81 0.65 0.52 0.86 0.25
12351 0.73 0.29 0.18 0.60 0.35 0.58
12352 0.99 0.43 0.72 0.13 0.37 0.84
12353 0.67 0.20 0.36 0.11 0.86 0.51
12354 0.70 0.84 0.76 0.19 0.46 0.89
;

data have2;
input id $ month;
datalines;
12345 5
12346 5
12348 5
12349 9
12351 5
12352 5
12353 5
;

proc transpose
  data=have1
  out=howitshouldbe (rename=(col1=cost))
;
by id;
var mon:;
run;

data have1_real;
set howitshouldbe;
month = input(scan(_name_,2,'_'),best.);
drop _name_;
run;

data want;
merge
  have1_real (in=h1 rename=(cost=_cost month=_month))
  have2 (in=h2)
;
by id;
if h1 and h2;
retain cost;
if first.id then cost = .;
if month = _month then cost = _cost;
if last.id;
actual = _cost - cost;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the first few steps are only there to beat your unusable data into shape (long format). The whole real work is done in the last step, which becomes very simple through intelligent design of the data structure.&lt;/P&gt;
&lt;P&gt;See Maxim 33.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 13:43:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cost-calculation/m-p/607676#M17583</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-27T13:43:33Z</dc:date>
    </item>
  </channel>
</rss>

