<?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: SAS: Insert multiple columns or rows in a specific position of an existing dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/398148#M96261</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97066"&gt;@yescobar&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Is there an equivalent for columns?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS datasets every record will have the same number of variables in the output. So if you are thinking of "conditional" variables then no.&lt;/P&gt;
&lt;P&gt;If you add any statemen using variable then that varaible will be created if it does not alread and have a missing value unless assigned one.&lt;/P&gt;
&lt;P&gt;Label BlankVar='this is a blank';&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;Format BlankVar best5.;&lt;/P&gt;
&lt;P&gt;or an attribute statement, using the variable name on an array reference, retain&amp;nbsp;or any number of other statements&lt;/P&gt;
&lt;P&gt;will add a variable and get a message in the log about variable blankvar has never been referenced if you do not assign a value.&lt;/P&gt;
&lt;P&gt;A conditional:&lt;/P&gt;
&lt;P&gt;If x=3 then newvar=5;&lt;/P&gt;
&lt;P&gt;will add newvar to all records but the only values would be when x=3 and have 5.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2017 15:42:30 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-09-22T15:42:30Z</dc:date>
    <item>
      <title>SAS: Insert multiple columns or rows in a specific position of an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/397973#M96221</link>
      <description>&lt;P&gt;Goal: Add one or more empty rows after a specific position in the dataset&lt;/P&gt;&lt;P&gt;Here's the work I've done so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data test_data;
    set sashelp.class;
    output;

    /* Add a blank line after row 5*/
    if _n_ = 5 then do;
        call missing(of _all_);
        output;
    end;

    /* Add 4 blank rows after row 7*/

    if _n_ = 7 then do;
        /* inserts a blank row after row 8 (7 original rows + 1 blank row) */
        call missing(of _all_);

        /*repeats the newly created blank rows: inserts 3 blank rows*/
        do i = 1 to 3;
            output;
        end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm still learning how to use SAS, but I "feel" like there's a better way to get to the same result, chiefly not having to use a for loop to insert multiple empty rows. I was wondering:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is there a better way to do this for rows?&lt;/LI&gt;&lt;LI&gt;Is there an equivalent for columns?&lt;/LI&gt;&lt;LI&gt;These rows/columns are being added more to fit a report format. The dataset doesn't need these blank rows/columns for its own sake. Is some PROC or REPORT function that achieves the same thing?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 01:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/397973#M96221</guid>
      <dc:creator>yescobar</dc:creator>
      <dc:date>2017-09-22T01:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Insert multiple columns or rows in a specific position of an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/398016#M96229</link>
      <description>&lt;P&gt;Firstly I'd NEVER insert empty rows or columns in a data set just to make a report "fit". It makes any further processing of the data much more difficult and is simply unnecessary. Proc Report is incredibly versatile and give you just about any layout you want - for example this paper shows how you can simulate empty columns in your report&amp;nbsp;&lt;A href="http://www2.sas.com/proceedings/sugi31/092-31.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi31/092-31.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also simulate blank lines based on a condition using similar techniques&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 08:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/398016#M96229</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-09-22T08:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Insert multiple columns or rows in a specific position of an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/398148#M96261</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97066"&gt;@yescobar&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Is there an equivalent for columns?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS datasets every record will have the same number of variables in the output. So if you are thinking of "conditional" variables then no.&lt;/P&gt;
&lt;P&gt;If you add any statemen using variable then that varaible will be created if it does not alread and have a missing value unless assigned one.&lt;/P&gt;
&lt;P&gt;Label BlankVar='this is a blank';&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;Format BlankVar best5.;&lt;/P&gt;
&lt;P&gt;or an attribute statement, using the variable name on an array reference, retain&amp;nbsp;or any number of other statements&lt;/P&gt;
&lt;P&gt;will add a variable and get a message in the log about variable blankvar has never been referenced if you do not assign a value.&lt;/P&gt;
&lt;P&gt;A conditional:&lt;/P&gt;
&lt;P&gt;If x=3 then newvar=5;&lt;/P&gt;
&lt;P&gt;will add newvar to all records but the only values would be when x=3 and have 5.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 15:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/398148#M96261</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-22T15:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Insert multiple columns or rows in a specific position of an existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/423299#M104077</link>
      <description>&lt;P&gt;Regarding your first question: Is there a better way to do this for rows? I have&amp;nbsp;not found a better solution on the web. However, I have used your solution to insert multiple rows between two existing rows (current and previous row).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Convert year/week variable to a date (Monday in week)
*/
%macro year_week_to_date(year_week, active_libname, table_input, table_output);
	proc sql;
		create table &amp;amp;active_libname..&amp;amp;table_output as
		select
		*,
		intnx(
			'week.2',
			(
				case when week(mdy(1, 1, input(substr(&amp;amp;year_week, 1, 4), 4.)), 'v') eq 1
				then intnx('week.1', mdy(1, 1, input(substr(&amp;amp;year_week, 1, 4), 4.)), 0)
				else
				mdy(1, 1, input(substr(&amp;amp;year_week, 1, 4), 4.))
				end
			),
			input(substr(&amp;amp;year_week, 5, 2), 2.)
		) as year_week_date
		format ddmmyyd10.
		from &amp;amp;active_libname..&amp;amp;table_input;
	quit;
%mend;

options mprint;
%year_week_to_date(year_week, work, any_table, temp0);

/*
Sort data
*/
proc sort data=work.temp0 out=work.temp1;
	by a b c d e year_week_date;
run;

/*
Flag the current row if the difference between the current and previous row (within group) is greater or equal to one week
*/
data work.temp2;
	set work.temp1;
	by a b c d e year_week_date;

	group = 0;
	delta = intck('week', lag(year_week_date), year_week_date) - 1;

	if lag(a) eq a
	and lag(b) eq b
	and lag(c) eq c
	and lag(d) eq d
	and lag(e) eq e then group = 1;

	if group eq 1 and delta eq 0 then break = 0;
	else if group eq 1 and delta ge 1 then break = 1;
	else break = 0;
run;

/*
Sort data
*/
proc sort data=work.temp2 out=work.temp3;
	by a b c d e descending year_week_date;
run;

/*
Insert new row(s) and update column values
Insert a maximum of 4 rows (business rule)
*/
data work.temp4;
	set work.temp3;
	by a b c d e descending year_week_date;
	output;
	if break eq 1 then
		do;
			do i = 1 to delta until(i = 4);
				* Flag a new row;
				break = 2;
				year_week_date = intnx('week.2', year_week_date, - 1);
				f = 'No delivery';
				g = 0;
				output;
			end;
		end;
run;

/*
Convert year_week_date to year/week and replace the old year/week variable
*/
data work.temp5;
	set work.temp4;
	year_week =
		put(
			year(year_week_date) - (month(year_week_date) = 1 and week(year_week_date, 'v') in(52, 53)) + (month(year_week_date) = 12 and week(year_week_date, 'v') = 1),
			4.
		)
		||
		put(
			week(year_week_date, 'v'),
			z2.
		);
run;

/*
Sort data
*/
proc sort data=work.temp5 out=work.output;
	by a b c d e year_week_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help &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;</description>
      <pubDate>Fri, 22 Dec 2017 21:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Insert-multiple-columns-or-rows-in-a-specific-position-of-an/m-p/423299#M104077</guid>
      <dc:creator>Hsej</dc:creator>
      <dc:date>2017-12-22T21:00:53Z</dc:date>
    </item>
  </channel>
</rss>

