<?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: Calculate variable in different rows and create a new variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870141#M343698</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439089"&gt;@cnn96&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this will do it.&lt;/P&gt;
&lt;P&gt;First use a select to calculate the time_span for each PatientID then add the calculated value with a join.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: Untested, because you didn't provide a data step to generate test data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table b as
    select distinct PatientID, count(*) as time_span
    from mylib.mytable
    group by PatientID;
quit;

proc sql;
  create table table_timespan as
    select a.*, b.time_span
    from mylib.mytable as a, b
    on a.PatientID = b.PatientID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 Apr 2023 11:46:55 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2023-04-17T11:46:55Z</dc:date>
    <item>
      <title>Calculate variable in different rows and create a new variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870136#M343696</link>
      <description>&lt;P&gt;Hello all.&lt;/P&gt;&lt;P&gt;I would like to complete the table on the left side with a variable time span based on the years.&lt;/P&gt;&lt;P&gt;I hope that you can help me.&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="SAS.png" style="width: 662px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/82758iEF2AC61AA19C65DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS.png" alt="SAS.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Apr 2023 11:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870136#M343696</guid>
      <dc:creator>cnn96</dc:creator>
      <dc:date>2023-04-17T11:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate variable in different rows and create a new variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870141#M343698</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439089"&gt;@cnn96&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this will do it.&lt;/P&gt;
&lt;P&gt;First use a select to calculate the time_span for each PatientID then add the calculated value with a join.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: Untested, because you didn't provide a data step to generate test data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table b as
    select distinct PatientID, count(*) as time_span
    from mylib.mytable
    group by PatientID;
quit;

proc sql;
  create table table_timespan as
    select a.*, b.time_span
    from mylib.mytable as a, b
    on a.PatientID = b.PatientID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Apr 2023 11:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870141#M343698</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-04-17T11:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate variable in different rows and create a new variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870143#M343700</link>
      <description>proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select id,year,range(year) as time_span&lt;BR /&gt; from have&lt;BR /&gt;  group by id ;&lt;BR /&gt;quit;</description>
      <pubDate>Mon, 17 Apr 2023 11:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870143#M343700</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-17T11:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate variable in different rows and create a new variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870146#M343701</link>
      <description>To get the time_span numbers listed in the "Aim:", I think the query should be &lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select id,year,(range(year)+1) as time_span&lt;BR /&gt;from have&lt;BR /&gt;group by id ;&lt;BR /&gt;quit;</description>
      <pubDate>Mon, 17 Apr 2023 12:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870146#M343701</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-17T12:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate variable in different rows and create a new variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870150#M343702</link>
      <description>&lt;P&gt;Thank you very much. This way it worked the way i wanted it to.&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Apr 2023 12:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-variable-in-different-rows-and-create-a-new-variable/m-p/870150#M343702</guid>
      <dc:creator>cnn96</dc:creator>
      <dc:date>2023-04-17T12:35:04Z</dc:date>
    </item>
  </channel>
</rss>

