<?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 new variable with long data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454626#M69996</link>
    <description>&lt;P&gt;Thanks so much for your prompt reply, Kurt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've put the code in as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data surgery;&lt;BR /&gt;SET surgery;&lt;BR /&gt;by id;&lt;BR /&gt;if first.idthen surgerynumber = 1;&lt;BR /&gt;else surgerynumber + 1;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC PRINT data='surgery';&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works in that there is a new variable surgerynumber = 1 to every id but for the 2nd surgery it jumps to surgery=3 (not 2). I have a dataset of 330 cases and even for 1 case that had 3 surgeries, with the new variable surgerynumber=1,3,4 (ie misses the 2). Not sure how to rectify this? Also some cases had no surgery (in the surgery column, it is just blank) but a 1 is put down next to the case with surgerynumber. Can we refine?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&lt;/P&gt;</description>
    <pubDate>Tue, 17 Apr 2018 05:00:33 GMT</pubDate>
    <dc:creator>lavienrose1</dc:creator>
    <dc:date>2018-04-17T05:00:33Z</dc:date>
    <item>
      <title>How to create a new variable with long data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454621#M69994</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apologies if this has been answered elsewhere. I am using SAS 9.4. trying to transpose my data from long to wide. I would like to create a new variable 'surgerynumber' to use in the id statement in proc transpose command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data is as:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;Type&lt;/TD&gt;&lt;TD&gt;Complication&lt;/TD&gt;&lt;TD&gt;Histo&lt;/TD&gt;&lt;TD&gt;timepoint&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;2/06/2009&lt;/TD&gt;&lt;TD&gt;resection&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Initial Diag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;2/08/2013&lt;/TD&gt;&lt;TD&gt;excision&lt;/TD&gt;&lt;TD&gt;Yes&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Before&amp;nbsp;radio&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;24/10/2012&lt;/TD&gt;&lt;TD&gt;resection&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Initial Diag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;26/02/2014&lt;/TD&gt;&lt;TD&gt;excision&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Before&amp;nbsp;radio&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;26/07/1994&lt;/TD&gt;&lt;TD&gt;excision&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Initial Diag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;Surgery&lt;/TD&gt;&lt;TD&gt;25/03/2011&lt;/TD&gt;&lt;TD&gt;excision&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No residual macroscopic disease&lt;/TD&gt;&lt;TD&gt;Initial Diag&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see that out of the 5 cases, only number 4 had surgery twice. So I would like to create a new variable 'surgerynumber' such that for case 1,2,3,5 it will be surgerynumber=1 and for case 4 it will be surgerynumber=1 for the first surgery and surgerynumber=2 for the second surgery.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would appreciate any solutions as to how to tackle the above - thanks&lt;/P&gt;</description>
      <pubDate>Tue, 17 Apr 2018 04:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454621#M69994</guid>
      <dc:creator>lavienrose1</dc:creator>
      <dc:date>2018-04-17T04:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a new variable with long data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454622#M69995</link>
      <description>&lt;P&gt;In a data step with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by id;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.id
then surgerynumber = 1;
else surgerynumber + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The incrementation statement in the else branch will automatically retain the variable.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Apr 2018 04:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454622#M69995</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-17T04:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a new variable with long data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454626#M69996</link>
      <description>&lt;P&gt;Thanks so much for your prompt reply, Kurt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've put the code in as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data surgery;&lt;BR /&gt;SET surgery;&lt;BR /&gt;by id;&lt;BR /&gt;if first.idthen surgerynumber = 1;&lt;BR /&gt;else surgerynumber + 1;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC PRINT data='surgery';&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works in that there is a new variable surgerynumber = 1 to every id but for the 2nd surgery it jumps to surgery=3 (not 2). I have a dataset of 330 cases and even for 1 case that had 3 surgeries, with the new variable surgerynumber=1,3,4 (ie misses the 2). Not sure how to rectify this? Also some cases had no surgery (in the surgery column, it is just blank) but a 1 is put down next to the case with surgerynumber. Can we refine?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&lt;/P&gt;</description>
      <pubDate>Tue, 17 Apr 2018 05:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454626#M69996</guid>
      <dc:creator>lavienrose1</dc:creator>
      <dc:date>2018-04-17T05:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a new variable with long data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454630#M69997</link>
      <description>&lt;P&gt;The code, applied to your original example data, works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',';
input ID $ Surgery $ date :ddmmyy10. Type :$10. Complication $ Histo :$15. timepoint :$20.;
format date ddmmyy10.;
cards;
1,Surgery,2/06/2009,resection,No,No residual macroscopic disease,Initial Diag 
2,Surgery,2/08/2013,excision,Yes,No residual macroscopic disease,Before radio 
3,Surgery,24/10/2012,resection,No,No residual macroscopic disease,Initial Diag 
4,Surgery,26/02/2014,excision,No,No residual macroscopic disease,Before radio 
4,Surgery,26/07/1994,excision,No,No residual macroscopic disease,Initial Diag 
5,Surgery,25/03/2011,excision,No,No residual macroscopic disease,Initial Diag 
;
run;

data want;
set have;
by id;
if first.id
then surgerynumber = 1;
else surgerynumber + 1;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Surgery          date      Type       Complication         Histo          timepoint      surgerynumber

1     Surgery    02/06/2009    resection        No          No residual mac    Initial Diag          1      
2     Surgery    02/08/2013    excision         Yes         No residual mac    Before radio          1      
3     Surgery    24/10/2012    resection        No          No residual mac    Initial Diag          1      
4     Surgery    26/02/2014    excision         No          No residual mac    Before radio          1      
4     Surgery    26/07/1994    excision         No          No residual mac    Initial Diag          2      
5     Surgery    25/03/2011    excision         No          No residual mac    Initial Diag          1      
&lt;/PRE&gt;
&lt;P&gt;You can see that surgerynumber = 2 is clearly there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Apr 2018 06:05:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454630#M69997</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-17T06:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a new variable with long data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454951#M70003</link>
      <description>&lt;P&gt;Thanks Kurt!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Apr 2018 22:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-a-new-variable-with-long-data/m-p/454951#M70003</guid>
      <dc:creator>lavienrose1</dc:creator>
      <dc:date>2018-04-17T22:47:47Z</dc:date>
    </item>
  </channel>
</rss>

