<?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: Creating a new column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686076#M24373</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input ID	amount;*	amount1;
cards;
12	2000	4000
167	2500	4000
256	1300	4000
3445	4000	4000
;

data want;
 set have;
 if _n_=1 then  set have(keep=amount rename=(amount=amount1)) point=nobs nobs=nobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 23 Sep 2020 14:59:23 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-09-23T14:59:23Z</dc:date>
    <item>
      <title>Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686066#M24371</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I am a new SAS user. I want a data with 2 columns 'ID' and 'amount' and want to create a new column 'amount1' that is the last observation's value in each line.&lt;/P&gt;&lt;P&gt;could you please tell me how can I do it?&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;amount&lt;/TD&gt;&lt;TD&gt;amount1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;167&lt;/TD&gt;&lt;TD&gt;2500&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;256&lt;/TD&gt;&lt;TD&gt;1300&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3445&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686066#M24371</guid>
      <dc:creator>Nini1</dc:creator>
      <dc:date>2020-09-23T14:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686076#M24373</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input ID	amount;*	amount1;
cards;
12	2000	4000
167	2500	4000
256	1300	4000
3445	4000	4000
;

data want;
 set have;
 if _n_=1 then  set have(keep=amount rename=(amount=amount1)) point=nobs nobs=nobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686076#M24373</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-09-23T14:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686078#M24374</link>
      <description>&lt;P&gt;I'm not sure I understood your question, but if I did, there are two approaches.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The lag function is designed to give you the value of a variable the previous time it was called.&amp;nbsp; If you call it every loop, it will give you the value from the previous row.&amp;nbsp; It is easy, but can get tricky with more complicated code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a retain statement is a little more work, but is much more flexible and not a tricky.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd;
  input id amount;
datalines;
12,2000
167,2500
256,1300
3445,4000
;
run;

data want_lag;
  set have;
  amount1 = lag(amount);
run;

data want_retain;
  set have;
  retain amount1 .;
  output;
  amount1 = amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686078#M24374</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-23T14:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686086#M24376</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/344950"&gt;@Nini1&lt;/a&gt;&amp;nbsp;and welcome.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I'm understanding you correctly, you could do the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	INFILE	DATALINES4
		FIRSTOBS	=	2
		DLM			=	'09'X
		DSD;
	INPUT	ID		$
			Amount	:	8.
			;
DATALINES4;
ID	amount	
12	2000	
167	2500	
256	1300	
3445	4000
;;;;
RUN;

DATA	Want;
	SET	Have	NOBS=Last_Observation;

	IF	_N_			=	1	THEN
		SET	Have	(RENAME=(Amount=Amount1))
			POINT	=	Last_Observation
			;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which would give you the following results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1600873821990.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49709i8FB59A04D73E17FE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1600873821990.png" alt="jimbarbour_0-1600873821990.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using a couple of "tricks" here:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; _N_ is an automatic variable provided by SAS.&amp;nbsp; If _N_ = 1, then it's the first record in the SET, and&amp;nbsp; _N_ = 1 ensures that the code controlled by the IF statement executes once and only once.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; I have not one but two SET statements.&amp;nbsp; The first SET statement drives the process and provides the current record, one record at a time to the rest of the program.&amp;nbsp; On the second SET statement, I'm using a RENAME parameter so that the value retrieved in Amount doesn't overlay the Amount from the current record.&amp;nbsp; I change the name from the second SET statement from Amount to Amount1.&lt;/P&gt;
&lt;P&gt;3. In the first SET statement, I have a NOBS parameter set to a variable, Last_Observation.&amp;nbsp; Each SAS dataset has two parts:&amp;nbsp; 1) a "header" that describes the data and includes things like the number of observations (NOBS) and 2) the data.&amp;nbsp; The NOBS parameter pulls the number of observations off the header portion of the SAS data set and stores it into the variable named Last_Observation.&lt;/P&gt;
&lt;P&gt;4.&amp;nbsp; In the second SET statement, I use a POINT parameter.&amp;nbsp; This parameter points to a particular record in SAS dataset&amp;nbsp;&lt;EM&gt;without&lt;/EM&gt; having to read through the rest of the dataset.&amp;nbsp; By using the value stored in Last_Observation, I point directly to the last record in the SAS dataset.&amp;nbsp; In so doing and in conjunction with the RENAME, I pull in the amount on the last record into Amount1.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope my explanation makes sense.&amp;nbsp; Please ask questions.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 15:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686086#M24376</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-23T15:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686094#M24377</link>
      <description>&lt;P&gt;What are you trying to do here? Get a total or summary statistic to each row for another calculation is my guess? Is there another way to identify that last row besides the 'last row'?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're trying to add a summary stat, see alternative methods here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/344950"&gt;@Nini1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;I am a new SAS user. I want a data with 2 columns 'ID' and 'amount' and want to create a new column 'amount1' that is the last observation's value in each line.&lt;/P&gt;
&lt;P&gt;could you please tell me how can I do it?&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;amount&lt;/TD&gt;
&lt;TD&gt;amount1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;12&lt;/TD&gt;
&lt;TD&gt;2000&lt;/TD&gt;
&lt;TD&gt;4000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;167&lt;/TD&gt;
&lt;TD&gt;2500&lt;/TD&gt;
&lt;TD&gt;4000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;256&lt;/TD&gt;
&lt;TD&gt;1300&lt;/TD&gt;
&lt;TD&gt;4000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3445&lt;/TD&gt;
&lt;TD&gt;4000&lt;/TD&gt;
&lt;TD&gt;4000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 15:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/686094#M24377</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-23T15:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/687161#M24506</link>
      <description>Thank you very much. Your replies helped me a lot.</description>
      <pubDate>Mon, 28 Sep 2020 10:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-a-new-column/m-p/687161#M24506</guid>
      <dc:creator>Nini1</dc:creator>
      <dc:date>2020-09-28T10:13:43Z</dc:date>
    </item>
  </channel>
</rss>

