<?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 add rows in sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644222#M192399</link>
    <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316738"&gt;@annypanny&lt;/a&gt;&amp;nbsp; &amp;nbsp;Here is a very simple solution that should be easy to follow for most. I have also written comments where necessary to help you understand the flow of the logic. I hope this is convenient enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Sc_no $ perf date :ddmmyy10.;
format date ddmmyy10.;
datalines;
A000855A  -0.28305 30112018
A000855A  0.49306 30092018
A000855A  2.14955 31032019
A000855A  2.53133 31072018
A000855A  4.19303 31012019
A000856A  -0.28305 30112018
A000856A  0.49306 30092018
A000856A  2.14955 31032019
A000856A  2.53133 31072018
A000856A  4.19303 31012019
;
/*Get the current month date and start date backtracking 61 months*/
%let  curr_dt=%sysfunc(intnx(mon,%sysfunc(today()),0,e));
%let  start_dt=%sysfunc(intnx(mon,&amp;amp;curr_dt,-61,e));
%put &amp;amp;=curr_dt;
%put &amp;amp;=start_dt;

/*Create the backtracked monthend date list*/
data backtracked_dates;
 date=&amp;amp;start_dt;
 do while(date&amp;lt;=&amp;amp;curr_dt);
  output;
  date=intnx('mon',date,1,'e');
 end;
 format date ddmmyy10.;
run;
/*Combine the dates with distinct Sc_no*/
/*This will give you a complete date list associated with each scn_no */
proc sql;
create table combine_scno_dates as
select *
from 
(select distinct Sc_no from have), backtracked_dates
order by Sc_no,date;
quit;
 
/*Now, Join the combine_scno_dates back with have to get the needed result*/
proc sql;
create table want as
select a.*,case when perf=. then 0 else perf end as perf
from combine_scno_dates a left join have b
on a.Sc_no=b.Sc_no and a.date=b.date
order by a.sc_no,a.date;
quit;
/*Print and check the results*/
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 Apr 2020 12:11:58 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-04-30T12:11:58Z</dc:date>
    <item>
      <title>how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643860#M192203</link>
      <description>&lt;P&gt;suppose i have a dataset&lt;/P&gt;
&lt;P&gt;Sc_no&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;perf&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date&lt;/P&gt;
&lt;P&gt;A000855A&amp;nbsp; -0.28305 30112018&lt;BR /&gt;A000855A&amp;nbsp; 0.49306 30092018&lt;BR /&gt;A000855A&amp;nbsp; 2.14955 31032019&lt;BR /&gt;A000855A&amp;nbsp; 2.53133 31072018&lt;BR /&gt;A000855A&amp;nbsp; 4.19303 31012019&lt;BR /&gt;A000856A&amp;nbsp; -0.28305 30112018&lt;BR /&gt;A000856A&amp;nbsp; 0.49306 30092018&lt;BR /&gt;A000856A&amp;nbsp; 2.14955 31032019&lt;BR /&gt;A000856A&amp;nbsp; 2.53133 31072018&lt;BR /&gt;A000856A&amp;nbsp; 4.19303 31012019&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My task is I want to create another dataset and having same field from the above dataset i.e.,&lt;/P&gt;
&lt;P&gt;Sc_no&amp;nbsp; perf&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;
&lt;P&gt;but there should be 61 rows for every distinct Sc_no. Example: for&amp;nbsp;A000855A Sc_no there should be 61 rows. Similarly if there is 2 distinct sc_no then there will be 61*2=122 rows will be there. And The date values should dynamic&amp;nbsp; i.e., there will be 61 rows of one distinct sc_no and the date should be dynamically change from 30th april 2020 i.e., the last date of this month to 61 month back i.e., 31st march 2015. How can I do and that should NOT be hard coded.&lt;/P&gt;
&lt;P&gt;Edited&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 13:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643860#M192203</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T13:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643870#M192208</link>
      <description>&lt;P&gt;So for&amp;nbsp;&lt;SPAN&gt;A000855A there are 5 obs right now. What should the values of perf and date be in the 56 obs to be added?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 11:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643870#M192208</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-29T11:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643872#M192210</link>
      <description>&lt;P&gt;No, from the above dataset there is only two distinct sc_no&amp;nbsp;A000855A and&amp;nbsp;A000856A&amp;nbsp; so for this 2 distinct sc_no there will be 61*2 rows will be there and in the perf. there will be perf=0 and date should be the last date of the month of that given sc_no. are you able to understand a bit now or I have to give more explanation sir.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 11:20:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643872#M192210</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T11:20:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643874#M192211</link>
      <description>&lt;P&gt;See if this gives you what you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Sc_no $ perf date :ddmmyy10.;
format date ddmmyy10.;
datalines;
A000855A  -0.28305 30112018
A000855A  0.49306 30092018
A000855A  2.14955 31032019
A000855A  2.53133 31072018
A000855A  4.19303 31012019
A000856A  -0.28305 30112018
A000856A  0.49306 30092018
A000856A  2.14955 31032019
A000856A  2.53133 31072018
A000856A  4.19303 31012019
;

data new (drop=_:);
    do until (last.Sc_no);
        set have;
        by Sc_no;
        if date &amp;gt; _date then _date=date;
    end;

    perf = 0;
    date = _date;

    do _N_ = 1 to 61;
        output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Apr 2020 11:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643874#M192211</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-29T11:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643876#M192212</link>
      <description>&lt;P&gt;the above code is running well but the only problem is that the date is similar in all of the rows, if you see in the given dataset there is different dates for same sc_no. How can I get that dates. every date is last date of the month i.e., 30 or 31st. but different month&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 11:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643876#M192212</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T11:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643880#M192213</link>
      <description>&lt;P&gt;Ok. So what date should be present in the first 10 obs of your desired data? Please be specific.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 11:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643880#M192213</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-29T11:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643891#M192220</link>
      <description>if you see in the above dataset sc_no is same but dates are different so in first 10 obs the date which are above in the dataset runs as same as the above dataset</description>
      <pubDate>Wed, 29 Apr 2020 12:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643891#M192220</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T12:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643901#M192228</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316738"&gt;@annypanny&lt;/a&gt;&amp;nbsp; Good morning. Upon reading the thread so far, is the following is perhaps what you are likely after?&amp;nbsp; All i did was modified&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;'s code a tiny bit&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input Sc_no $ perf date :ddmmyy10.;
format date ddmmyy10.;
datalines;
A000855A  -0.28305 30112018
A000855A  0.49306 30092018
A000855A  2.14955 31032019
A000855A  2.53133 31072018
A000855A  4.19303 31012019
A000856A  -0.28305 30112018
A000856A  0.49306 30092018
A000856A  2.14955 31032019
A000856A  2.53133 31072018
A000856A  4.19303 31012019
;

data new (drop=_:);
 do _n_= 1 by 1 until (last.Sc_no);
  set have;
  by Sc_no;
  if date &amp;gt; _date then _date=date;
  output;
 end;
 perf = 0;
 date = _date;
 do _n_=_n_+1 to 61;
  output;
 end;
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 12:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643901#M192228</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-29T12:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643932#M192247</link>
      <description>I want dynamic date i.e., there will be 61 rows of one distinct sc_no and the date should be dynamically change from 30th april 2020 i.e., the last date of this month to 61 month back i.e., 31st march 2015. what logic can help in this? thanks in advance</description>
      <pubDate>Wed, 29 Apr 2020 13:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643932#M192247</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T13:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643935#M192248</link>
      <description>&lt;P&gt;Alright, I see that explanation seems to start making sense of the logic. One more request plz. Can you paste your &lt;STRONG&gt;expected output&lt;/STRONG&gt; for one distinct value i.e.&amp;nbsp;A000855A to make sure what's on our mind aligns with your expectations. Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 13:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643935#M192248</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-29T13:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643939#M192252</link>
      <description>&lt;P&gt;sc_no&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pref&amp;nbsp; Date&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 31032015&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 30042015&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 31052015&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 30062015&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 31072015&lt;/P&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 31032020&lt;/P&gt;
&lt;P&gt;A000855A 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 30042020&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in Advance&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 13:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643939#M192252</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T13:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643975#M192274</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316738"&gt;@annypanny&lt;/a&gt;&amp;nbsp; Alright, I would have liked you to provide the full listing of one distinct&amp;nbsp;Sc_no expected result to save each other's time. Never mind. Looking at your expected result, the following is what I comprehend.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have a set of sc_no that has various unique month end dates and its associated perf_no. You want to backtrack from the current month for a period of 61 months or in other words 61 month end dates. Some month end dates within those 61 month end dates are likely to exist in the input dataset with a valid non zero&amp;nbsp; perf number associated with it. And for those non existing dates in the input, you want the perf value to assigned as zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the above description is correct, the following should do:-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input Sc_no $ perf date :ddmmyy10.;
format date ddmmyy10.;
datalines;
A000855A  -0.28305 30112018
A000855A  0.49306 30092018
A000855A  2.14955 31032019
A000855A  2.53133 31072018
A000855A  4.19303 31012019
A000856A  -0.28305 30112018
A000856A  0.49306 30092018
A000856A  2.14955 31032019
A000856A  2.53133 31072018
A000856A  4.19303 31012019
;
/*Get the current month date and start date backtracking 61 months*/
%let  curr_dt=%sysfunc(intnx(mon,%sysfunc(today()),0,e));
%let  start_dt=%sysfunc(intnx(mon,&amp;amp;curr_dt,-61,e));
%put &amp;amp;=curr_dt;
%put &amp;amp;=start_dt;
/*Load the available dates from the input in a look up hash table*/
/*Loop through the 62 months and assign the failed look up with existing as zero*/
data new ;
 if _n_=1 then do;
  dcl hash H () ;
  h.definekey  ("date") ;
  h.definedata ("perf") ;
  h.definedone () ;
 end;
 do until (last.Sc_no);
  set have;
  by Sc_no;
  h.add();
 end;
 do _n_=0 by 1 until(date=&amp;amp;curr_dt);
  date=intnx('mon',&amp;amp;start_dt,_n_,'e');
  if h.find() ne 0 then perf=0;
  output;
 end;
 h.clear();
run;
proc print noobs;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 14:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643975#M192274</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-29T14:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643988#M192283</link>
      <description>thank you very much sir</description>
      <pubDate>Wed, 29 Apr 2020 14:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/643988#M192283</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-29T14:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644190#M192383</link>
      <description>can we do it with a simple do loop logic as the code is not so simple and as a basic learnr the code seems to be tough to undesrtand</description>
      <pubDate>Thu, 30 Apr 2020 10:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644190#M192383</guid>
      <dc:creator>annypanny</dc:creator>
      <dc:date>2020-04-30T10:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644213#M192394</link>
      <description>&lt;P&gt;Sure, I have just reached my office. I will respond with an easy solution perhaps after breakfast. Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 11:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644213#M192394</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-30T11:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to add rows in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644222#M192399</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316738"&gt;@annypanny&lt;/a&gt;&amp;nbsp; &amp;nbsp;Here is a very simple solution that should be easy to follow for most. I have also written comments where necessary to help you understand the flow of the logic. I hope this is convenient enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Sc_no $ perf date :ddmmyy10.;
format date ddmmyy10.;
datalines;
A000855A  -0.28305 30112018
A000855A  0.49306 30092018
A000855A  2.14955 31032019
A000855A  2.53133 31072018
A000855A  4.19303 31012019
A000856A  -0.28305 30112018
A000856A  0.49306 30092018
A000856A  2.14955 31032019
A000856A  2.53133 31072018
A000856A  4.19303 31012019
;
/*Get the current month date and start date backtracking 61 months*/
%let  curr_dt=%sysfunc(intnx(mon,%sysfunc(today()),0,e));
%let  start_dt=%sysfunc(intnx(mon,&amp;amp;curr_dt,-61,e));
%put &amp;amp;=curr_dt;
%put &amp;amp;=start_dt;

/*Create the backtracked monthend date list*/
data backtracked_dates;
 date=&amp;amp;start_dt;
 do while(date&amp;lt;=&amp;amp;curr_dt);
  output;
  date=intnx('mon',date,1,'e');
 end;
 format date ddmmyy10.;
run;
/*Combine the dates with distinct Sc_no*/
/*This will give you a complete date list associated with each scn_no */
proc sql;
create table combine_scno_dates as
select *
from 
(select distinct Sc_no from have), backtracked_dates
order by Sc_no,date;
quit;
 
/*Now, Join the combine_scno_dates back with have to get the needed result*/
proc sql;
create table want as
select a.*,case when perf=. then 0 else perf end as perf
from combine_scno_dates a left join have b
on a.Sc_no=b.Sc_no and a.date=b.date
order by a.sc_no,a.date;
quit;
/*Print and check the results*/
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Apr 2020 12:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-rows-in-sas/m-p/644222#M192399</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-30T12:11:58Z</dc:date>
    </item>
  </channel>
</rss>

