<?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: How to create a variable corresponding to the observations of a level of another variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907184#M40563</link>
    <description>&lt;P&gt;Similar method, using a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.id);
  set have;
  by id;
  pre_cost = sum(pre_cost,ifn(pre_post_period = 1,cost,0));
  post_cost = sum(post_cost,ifn(pre_post_period = 2,cost,0));
end;
do until (last.id);
  set have;
  by id;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 10 Dec 2023 06:56:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-12-10T06:56:02Z</dc:date>
    <item>
      <title>How to create a variable corresponding to the observations of a level of another variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907181#M40561</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;I have a dataset like in the table below. Please, how do I create pre_cost and post_cost variables such that their values are the sum of costs corresponding to the pre-era (1) and post-era (2) respectively? Thank you so much.&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;id&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;pre_post_period&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;cost&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;pre_cost&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;post_cost&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;001&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;150&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;001&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;150&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;001&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;150&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;002&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;150&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;350&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;75&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;002&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;200&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;350&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;75&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;002&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;350&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;75&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;002&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;45&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;350&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;75&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 10 Dec 2023 04:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907181#M40561</guid>
      <dc:creator>Banke</dc:creator>
      <dc:date>2023-12-10T04:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable corresponding to the observations of a level of another variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907182#M40562</link>
      <description>&lt;P&gt;Since the data are sorted by ID, you can read each ID twice.&amp;nbsp; In the first pass, accumulate total pre-costs and total post_costs.&amp;nbsp; In the second pass, retrieve those totals and output the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the absence of sample data in the form of a working DATA step, this code is untested:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (in=firstpass)  have (in=secondpass);
  by id ;
  array _cost_sums {2} _temporary_;

  if first.id then call missing(of _cost_sums{*});
  if firstpass then _cost_sums{pre_post_period}+cost;

  if secondpass;
  pre_cost_sums=_cost_sums{1};
  post_cost_sums=_cost_sums{2};
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Dec 2023 05:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907182#M40562</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-10T05:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable corresponding to the observations of a level of another variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907184#M40563</link>
      <description>&lt;P&gt;Similar method, using a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.id);
  set have;
  by id;
  pre_cost = sum(pre_cost,ifn(pre_post_period = 1,cost,0));
  post_cost = sum(post_cost,ifn(pre_post_period = 2,cost,0));
end;
do until (last.id);
  set have;
  by id;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Dec 2023 06:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907184#M40563</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-10T06:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable corresponding to the observations of a level of another variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907186#M40564</link>
      <description>&lt;P&gt;Or a slight modification of the interleave method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set
  have (in=firstpass)
  have (in=secondpass)
;
by id;
retain pre_cost post_cost;
if first.id
then do;
  pre_cost = 0;
  post_cost = 0;
end;
if firstpass
then do;
  if pre_post_period = 1
  then pre_cost + cost;
  else post_cost + cost;
end;
if secondpass;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Dec 2023 07:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-variable-corresponding-to-the-observations-of-a/m-p/907186#M40564</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-10T07:01:49Z</dc:date>
    </item>
  </channel>
</rss>

