<?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: merging data in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673877#M202801</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;From what you have given to this thread,I assume following is expected.&lt;BR /&gt;1. Group by cownumber and selection number and summate seconds by each group.&lt;BR /&gt;2. However datetime should hold first observation of each group.&lt;/P&gt;&lt;P&gt;If this is your expectation, then SAS code below should do what is necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* input dataset */
data test(drop=datetm rename=(datetm1=datetm));
length cownum 8 datetm $ 16 seconds 8 selectionnm 8;
infile datalines dsd dlm="09"x;
input cownum  datetm $ seconds  selectionnm;
/* As datetime is in character,it needs to be traslated into datetime */
datetm1=input(datetm,datetime.);
format datetm1 nldatm48.;
cards;
11	13MAR20:09:10:41	10	1
11	13MAR20:09:10:55	25	1
11	13MAR20:09:11:41	10	1
11	13MAR20:09:12:41	35	2
14	13MAR20:09:10:41	10	1
14	13MAR20:09:12:41	25	2
14	13MAR20:09:13:41	24	3
14	13MAR20:09:14:41	35	3
;
run;

/* sort by cownum,selection */
proc sort data=work.test;
	by cownum selectionnm;
run;

option locale=en_US;
/* summate by cownum selection number */
data test2(drop=seconds datetm rename=(sumsec=seconds timegrp=datetm));
length cownum 8 datetm 8 seconds 8 selectionnm 8 timegrp $48;
	set work.test;
		by cownum selectionnm;
/* 	first retain sumsec variable and timegrp variable for grouping(seconds and datetime)*/
	retain sumsec 0;
	retain timegrp "";

/* set to 0 when obs hits first selection number group	 */
/* 	datetime should hold the first obs for each group */
	if first.selectionnm then do;
		sumsec=0;
		timegrp=put(datetm,nldatm48.);
	end;
/* 	summate seconds by cownum selection number group */
	sumsec=sumsec+seconds;
	
/* output at the end of each group */
	if last.selectionnm then do;
		output;
	end;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Following SAS retain statement link should explain above sample.(Example3)&lt;/P&gt;&lt;P&gt;&lt;A title="SAS retain statement link" href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=ja#p0ooiohfq20an9n1qrt7ddev0rql" target="_self"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=ja#p0ooiohfq20an9n1qrt7ddev0rql&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 01 Aug 2020 07:20:36 GMT</pubDate>
    <dc:creator>hhinohar</dc:creator>
    <dc:date>2020-08-01T07:20:36Z</dc:date>
    <item>
      <title>merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673680#M202710</link>
      <description>&lt;P&gt;Hi everbody,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to transform my dataset for further analysis, as in the document attached.&lt;/P&gt;&lt;P&gt;The goal is to get the data together with the same selectionnumber. The cownumber and the datetime needs do stay the same, but the seconds need to be summated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't get this working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which code / sas function is needed for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 11:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673680#M202710</guid>
      <dc:creator>Joostvanerp</dc:creator>
      <dc:date>2020-07-31T11:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673703#M202719</link>
      <description>&lt;P&gt;Most people will not download and open Excel files. It is a security threat. Please provide data according to these instructions:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 13:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673703#M202719</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-31T13:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673718#M202725</link>
      <description>&lt;P&gt;This are the tables in pdf&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 14:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673718#M202725</guid>
      <dc:creator>Joostvanerp</dc:creator>
      <dc:date>2020-07-31T14:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673774#M202747</link>
      <description>&lt;P&gt;Best is to provide example data in the form of data step code. With a data step we can see at a glance the names and types of variables (common sources of confusion when looking at PICTURES) as well being able to run code. Only include variables actually needed for the problem at hand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &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; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 15:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673774#M202747</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-31T15:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673778#M202751</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/336631"&gt;@Joostvanerp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This are the tables in pdf&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In order to help you write code to solve your problem, the data needs to be in SAS data step code so we can work with the exact same data that you have.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 16:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673778#M202751</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-31T16:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673877#M202801</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;From what you have given to this thread,I assume following is expected.&lt;BR /&gt;1. Group by cownumber and selection number and summate seconds by each group.&lt;BR /&gt;2. However datetime should hold first observation of each group.&lt;/P&gt;&lt;P&gt;If this is your expectation, then SAS code below should do what is necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* input dataset */
data test(drop=datetm rename=(datetm1=datetm));
length cownum 8 datetm $ 16 seconds 8 selectionnm 8;
infile datalines dsd dlm="09"x;
input cownum  datetm $ seconds  selectionnm;
/* As datetime is in character,it needs to be traslated into datetime */
datetm1=input(datetm,datetime.);
format datetm1 nldatm48.;
cards;
11	13MAR20:09:10:41	10	1
11	13MAR20:09:10:55	25	1
11	13MAR20:09:11:41	10	1
11	13MAR20:09:12:41	35	2
14	13MAR20:09:10:41	10	1
14	13MAR20:09:12:41	25	2
14	13MAR20:09:13:41	24	3
14	13MAR20:09:14:41	35	3
;
run;

/* sort by cownum,selection */
proc sort data=work.test;
	by cownum selectionnm;
run;

option locale=en_US;
/* summate by cownum selection number */
data test2(drop=seconds datetm rename=(sumsec=seconds timegrp=datetm));
length cownum 8 datetm 8 seconds 8 selectionnm 8 timegrp $48;
	set work.test;
		by cownum selectionnm;
/* 	first retain sumsec variable and timegrp variable for grouping(seconds and datetime)*/
	retain sumsec 0;
	retain timegrp "";

/* set to 0 when obs hits first selection number group	 */
/* 	datetime should hold the first obs for each group */
	if first.selectionnm then do;
		sumsec=0;
		timegrp=put(datetm,nldatm48.);
	end;
/* 	summate seconds by cownum selection number group */
	sumsec=sumsec+seconds;
	
/* output at the end of each group */
	if last.selectionnm then do;
		output;
	end;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Following SAS retain statement link should explain above sample.(Example3)&lt;/P&gt;&lt;P&gt;&lt;A title="SAS retain statement link" href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=ja#p0ooiohfq20an9n1qrt7ddev0rql" target="_self"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=ja#p0ooiohfq20an9n1qrt7ddev0rql&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Aug 2020 07:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-a-dataset/m-p/673877#M202801</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-08-01T07:20:36Z</dc:date>
    </item>
  </channel>
</rss>

