<?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: Create an observation from multiple rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949444#M371400</link>
    <description>&lt;P&gt;Why you have to use only one data step to get all of these?&lt;/P&gt;
&lt;P&gt;It is totally useless and nonsense .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=sale;
filename sale temp;
parmcards;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
20/Aug/2023 37800
1/Dec/2023 21500
30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;




data have;
infile sale truncover end=last;
length name $ 200;
retain name;
input ;
name=coalescec(scan(_infile_,-2,'"'),name);
date=input(scan(_infile_,-2,' '),date11.);
sale=input(scan(_infile_,-1,' '),best.);
format date last_sale_date date11.;

array n{999999} $ 200 _temporary_;
array d{999999}  _temporary_;
array s{999999}  _temporary_;
n{_n_}=name;
d{_n_}=date;
s{_n_}=sale;
if last then do;
  do i=1 to _n_;
   last_sale_date=max(last_sale_date,d{i});
   number_of_sale+1;
   running_sale_total+s{i};
   if n{i} ne n{i+1} then do;
     new_name=n{i};
     days_between_last_sale_31dec2023='31dec2023'd-last_sale_date;
     output;
	 call missing(last_sale_date,number_of_sale,running_sale_total);
   end;
  end;
end;
keep new_name days_between_last_sale_31dec2023 last_sale_date number_of_sale running_sale_total;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1730254045609.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101874i61C1BA21DD28DBA9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1730254045609.png" alt="Ksharp_0-1730254045609.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2024 02:07:35 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-10-30T02:07:35Z</dc:date>
    <item>
      <title>Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949221#M371345</link>
      <description>&lt;P&gt;Greetings! Below are the data sets to compute.&lt;/P&gt;&lt;P&gt;"Mary Chan" 3/Jul/2023 45600&lt;BR /&gt;20/Aug/2023 37800&lt;BR /&gt;1/Dec/2023 21500&lt;BR /&gt;30/Aug/2023 42000&lt;BR /&gt;"John Tam" 12/Sep/2023 35000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output should be like:&lt;/P&gt;&lt;P&gt;Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;No. of visits&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Total Spent&lt;/P&gt;&lt;P&gt;Mary Chan&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;216550&lt;/P&gt;&lt;P&gt;John Tam&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;35000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea? Thanks guys!!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 12:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949221#M371345</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-28T12:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949224#M371347</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470764"&gt;@Mike_Chan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Greetings! Below are the data sets to compute.&lt;/P&gt;
&lt;P&gt;"Mary Chan" 3/Jul/2023 45600&lt;BR /&gt;20/Aug/2023 37800&lt;BR /&gt;1/Dec/2023 21500&lt;BR /&gt;30/Aug/2023 42000&lt;BR /&gt;"John Tam" 12/Sep/2023 35000&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Have you already created a SAS data set from the above raw data? If so, please share the code. If not, then is the question really how to read this data into SAS?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems like the dates are irrelevant to the creation of the desired output data set, is that correct?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 13:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949224#M371347</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-28T13:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949229#M371349</link>
      <description>&lt;P&gt;Thanks for ur reply! This was the simplified output above. The full output should be 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="Mike_Chan_0-1730122005051.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101834i57637BFFBF4C8D95/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_0-1730122005051.png" alt="Mike_Chan_0-1730122005051.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mike_Chan_1-1730122070268.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101835iFCB7AFD80900706E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_1-1730122070268.png" alt="Mike_Chan_1-1730122070268.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;(i think i can handle the case regarding to dates so i didnt include it&amp;nbsp; on my question!) Thanks so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 13:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949229#M371349</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-28T13:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949230#M371350</link>
      <description>&lt;P&gt;This does not seem to answer my other questions.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 13:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949230#M371350</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-28T13:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949237#M371351</link>
      <description>&lt;P&gt;The dataset is relevant to the output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The logic is firstly record the name of the customer, then count how many sales were made by the customer (can be by the number of date or number of sales made). Finally, compute the days between last sales and 31 Dec 2023.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are the code im trying but seems not work:&lt;/P&gt;&lt;DIV&gt;data Totalsales;&lt;/DIV&gt;&lt;DIV&gt;input name $quote20. @;&lt;/DIV&gt;&lt;DIV&gt;format var1 date11.;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;s1 = substr(name,1,1);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If ('A' &amp;lt;= s1 &amp;lt;= 'Z') then do;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if _n_ ^=1 then output;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;input var1&amp;nbsp; sales;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;end;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if endfile=1 then output;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;"David Wong" 1/Aug/2023 13200&lt;/DIV&gt;&lt;DIV&gt;"Brian Leung" 15/Sep/2023 23450&lt;/DIV&gt;&lt;DIV&gt;10/Sep/2023 33000&lt;/DIV&gt;&lt;DIV&gt;"Mary Chan" 3/Jul/2023 45600&lt;/DIV&gt;&lt;DIV&gt;20/Aug/2023 37800&lt;/DIV&gt;&lt;DIV&gt;1/Dec/2023 21500&lt;/DIV&gt;&lt;DIV&gt;30/Aug/2023 42000&lt;/DIV&gt;&lt;DIV&gt;"John Tam" 12/Sep/2023 35000&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;</description>
      <pubDate>Mon, 28 Oct 2024 14:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949237#M371351</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-28T14:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949239#M371352</link>
      <description>&lt;P&gt;Assuming you have a dataset named HAVE with variables named NAME and SPENT then you should just use PROC SUMMARY to calculate your desired results.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway ;
   class name;
   var spent ;
   output out=want n=COUNT sum=Total_Spent ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 14:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949239#M371352</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-28T14:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949243#M371353</link>
      <description>&lt;P&gt;IF you have the NAME on every observation then a suggested update to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s solution. If you data does not have the name on each observation that is the first step before summarizing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Proc summary data=have nway;
   class name;
   var spent date;
   output out=need(drop=_:) sum(spent)=spentsum n(spent)=NumberSales max(date)=latestdate
;
run;&lt;BR /&gt;Data want;&lt;BR /&gt;   set need;&lt;BR /&gt;   daysbetween = '31Dec2023'd - latestdate;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;I wouldn't call such a total a "running sum" as that would generally be interpreted as incremented per observation in a data set with more than one observation per name or similar grouping variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really should make sure that your resultant date variables have a suitable display format assigned.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 15:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949243#M371353</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-28T15:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949246#M371354</link>
      <description>&lt;P&gt;sorry for the misleading. My aim is to develop a data step to generate the output (provided in the question). I have worked part of it (Can see in the comment section) but I was totally struggled.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 15:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949246#M371354</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-28T15:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949253#M371355</link>
      <description>&lt;P&gt;Non-tested code since you disn't provide raw data as a data step/datalines:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select name, 
	     max(sales_date) as last_sales_date,
	     count(*) as no_of_sales,
	     sum(sales) as run_sales_tot,
	     '31Dec2023'd - calculated last_sales_date as no_of_days_last_to_31dec2023
	from have
	group by name
	;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Oct 2024 15:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949253#M371355</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-10-28T15:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949265#M371358</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470764"&gt;@Mike_Chan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;sorry for the misleading. My aim is to develop a data step to generate the output (provided in the question). I have worked part of it (Can see in the comment section) but I was totally struggled.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you actually want to rewrite the procedures that SAS has spent years developing so they are fast and relatively easy to use? Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you changed the requirement from the initial question to the second output. What else may creep in that isn't really amenable to data step processing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Provide example data in the form of a working data step and we can show how to write a data step but really, other procedures are the way to go. What works for one data set may require a great deal of additional logic/coding when you add one variable or condition. So all the requirements and conditions have be stated up front as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 16:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949265#M371358</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-28T16:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949284#M371367</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470764"&gt;@Mike_Chan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Greetings! Below are the data sets to compute.&lt;/P&gt;
&lt;P&gt;"Mary Chan" 3/Jul/2023 45600&lt;BR /&gt;20/Aug/2023 37800&lt;BR /&gt;1/Dec/2023 21500&lt;BR /&gt;30/Aug/2023 42000&lt;BR /&gt;"John Tam" 12/Sep/2023 35000&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So it &lt;U&gt;seems&lt;/U&gt; you have a dataset with three character variables; in the first row of each group, you have the first variable populated with a name, but in the following rows everything shifts to the left, so the third variable is empty.&lt;/P&gt;
&lt;P&gt;If that is not what you have, please clear up any ambiguity by posting your "have" dataset in a working DATA step with DATALINES that recreates your dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 17:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949284#M371367</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-10-28T17:56:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949286#M371368</link>
      <description>&lt;P&gt;Thx for ur reply. It is an assignment from my school so I guess this is the challenging part.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 18:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949286#M371368</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-28T18:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949289#M371369</link>
      <description>&lt;P&gt;In which form did you get that assignment? Particularly, how is the source data contained there? File attachment, text, picture, something else? Or do you have a dataset already stored on a server?&lt;/P&gt;
&lt;P&gt;The more (clear) information you provide about your task, the faster you will get answers leading to a solution. By forcing us to make guesses, you waste time, yours and ours.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 18:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949289#M371369</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-10-28T18:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949311#M371370</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x $80.;
name=scan(x,-2,'"');
date=input(scan(x,-2,' '),date11.);
sale=input(scan(x,-1,' '),best.);
format date date11.;
drop x;
datalines;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
20/Aug/2023 37800
1/Dec/2023 21500
30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;
run;
data temp;
 set have;
 length new_name $ 80;
 retain new_name;
 if not missing(name) then new_name=name;
 drop name;
run;
proc summary data=temp nway;
class new_name;
var sale;
output out=want(drop=_type_ rename=(_freq_=no_of_visit)) sum=Toatl_Spent;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2024 03:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949311#M371370</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-29T03:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949331#M371377</link>
      <description>&lt;P&gt;thx for your distribution. But the output should be one observation per person.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output should be:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mike_Chan_1-1730192386257.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101843i6A5657E663AC0EB9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_1-1730192386257.png" alt="Mike_Chan_1-1730192386257.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mike_Chan_0-1730192356517.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101842i8EAE88738904F6D3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_0-1730192356517.png" alt="Mike_Chan_0-1730192356517.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am attempting this direction but its stuck rn.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Totalsales;
input name $quote20. @;

s1 = substr(name,1,1);
If ('A' &amp;lt;= s1 &amp;lt;= 'Z') then do;
          if  _n_ ^=1 then do;

datalines;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
  10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
20/Aug/2023 37800
    1/Dec/2023 21500
 30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;
run;&lt;/CODE&gt;&lt;/PRE&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>Tue, 29 Oct 2024 09:04:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949331#M371377</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-29T09:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949334#M371378</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mike_Chan_0-1730192752332.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101844i3893A88C259B3CD7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_0-1730192752332.png" alt="Mike_Chan_0-1730192752332.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mike_Chan_1-1730192761593.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101845i6159DD9736197B69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mike_Chan_1-1730192761593.png" alt="Mike_Chan_1-1730192761593.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is the question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Totalsales;
input name $quote20. @;

s1 = substr(name,1,1);

If ('A' &amp;lt;= s1 &amp;lt;= 'Z') then do;

datalines;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
  10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
20/Aug/2023 37800
    1/Dec/2023 21500
 30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am going on this direction but im stuck.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 09:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949334#M371378</guid>
      <dc:creator>Mike_Chan</dc:creator>
      <dc:date>2024-10-29T09:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949339#M371380</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470764"&gt;@Mike_Chan&lt;/a&gt;&amp;nbsp;I'm not sure what the creator of this exercise has been thinking. No SAS programmer with some experience would do this in a single data step. It just complicates and convolutes the process.&lt;/P&gt;
&lt;P&gt;I'm normally not providing full code for an exercise but making an exception for this one given all the unnecessary complications. Please spend the time to understand what the code does (if you copy/paste the code into an AI like Copilot then you'll get already a lot of explanation - rest is in the SAS docu).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create text file with sample data */
filename src_data temp;
data _null_;
  file src_data;
  infile datalines;
  input;
  put _infile_;
  datalines;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
  10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
 20/Aug/2023 37800
     1/Dec/2023 21500
   30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;
run;

/* single data step approach as demanded */
data demo;
  attrib
    name            format=$20.    label='Name'
    last_sales_date format=date9.  label='Last sales date'
    no_of_sales     format=best32. label='Number of sales'
    sales_total     format=best32. label='Running sales total'
    days_diff       format=best32. label='Days between last sales and 31 Dec 2023'
    ;
  keep 
    name
    last_sales_date
    no_of_sales    
    sales_total    
    days_diff      
    ;

  array a_dts{20} 8 _temporary_;
  array a_sal{20} 8 _temporary_;
  retain name sales_total;

  infile src_data dsd dlm=' ' truncover end=eof;
  input _x $1. @;

  if missing(_x) then 
    do;
      no_of_sales+1;
      _infile_=left(_infile_);
      input @1 a_dts[no_of_sales]:date9. a_sal[no_of_sales]:best32.;
    end;

  else
    do;
      if _n_&amp;gt;1 then
        do;
          last_sales_date=max(of a_dts[*]);
          days_diff='31dec2023'd-last_sales_date;
          sales_total=sum(sales_total, of a_sal[*]);
          output;
          call missing(of a_dts[*], of a_sal[*]); 
        end;

      no_of_sales=1;
      input @1 name:$20. a_dts[no_of_sales]:date9. a_sal[no_of_sales]:best32.;
    end;

  if eof then
    do;
      last_sales_date=max(of a_dts[*]);
      days_diff='31dec2023'd-last_sales_date;
      sales_total=sum(sales_total, of a_sal[*]);
      output;
    end;
run;

proc print data=demo label;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1730199079031.png" style="width: 766px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101847iE62D4DC7A7EBE51B/image-dimensions/766x136?v=v2" width="766" height="136" role="button" title="Patrick_0-1730199079031.png" alt="Patrick_0-1730199079031.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 11:14:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949339#M371380</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-10-29T11:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create an observation from multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949444#M371400</link>
      <description>&lt;P&gt;Why you have to use only one data step to get all of these?&lt;/P&gt;
&lt;P&gt;It is totally useless and nonsense .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=sale;
filename sale temp;
parmcards;
"David Wong" 1/Aug/2023 13200
"Brian Leung" 15/Sep/2023 23450
10/Sep/2023 33000
"Mary Chan" 3/Jul/2023 45600
20/Aug/2023 37800
1/Dec/2023 21500
30/Aug/2023 42000
"John Tam" 12/Sep/2023 35000
;




data have;
infile sale truncover end=last;
length name $ 200;
retain name;
input ;
name=coalescec(scan(_infile_,-2,'"'),name);
date=input(scan(_infile_,-2,' '),date11.);
sale=input(scan(_infile_,-1,' '),best.);
format date last_sale_date date11.;

array n{999999} $ 200 _temporary_;
array d{999999}  _temporary_;
array s{999999}  _temporary_;
n{_n_}=name;
d{_n_}=date;
s{_n_}=sale;
if last then do;
  do i=1 to _n_;
   last_sale_date=max(last_sale_date,d{i});
   number_of_sale+1;
   running_sale_total+s{i};
   if n{i} ne n{i+1} then do;
     new_name=n{i};
     days_between_last_sale_31dec2023='31dec2023'd-last_sale_date;
     output;
	 call missing(last_sale_date,number_of_sale,running_sale_total);
   end;
  end;
end;
keep new_name days_between_last_sale_31dec2023 last_sale_date number_of_sale running_sale_total;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1730254045609.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101874i61C1BA21DD28DBA9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1730254045609.png" alt="Ksharp_0-1730254045609.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 02:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-an-observation-from-multiple-rows/m-p/949444#M371400</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-30T02:07:35Z</dc:date>
    </item>
  </channel>
</rss>

