<?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: Incrementing values of variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570707#M160955</link>
    <description>&lt;P&gt;Here's a less elegant but simple set of steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
run;
proc sort data=spend; by custid week; run; 
proc sql noprint;
	select distinct custid into :custids separated by ","
	from spend;
quit;
/* create empty table*/
data empty (drop=i);
	length custid week 8.;
	do i = 1 to countc("&amp;amp;custids.",",") +1;
		do week=1 to 7;
			custid = scan("&amp;amp;custids.",i,",");
			output;
		end;
	end;
run;
/*populate values into formerly empty table*/
data want;
	merge empty spend;
	by custid week;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Jul 2019 19:01:46 GMT</pubDate>
    <dc:creator>noling</dc:creator>
    <dc:date>2019-07-02T19:01:46Z</dc:date>
    <item>
      <title>Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570598#M160923</link>
      <description>&lt;P&gt;I have a dataset spend with numeric values -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data spend;&lt;BR /&gt;input custid week spends;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 4365&lt;BR /&gt;1 2 457&lt;BR /&gt;1 3 124&lt;BR /&gt;1 4 35547&lt;BR /&gt;2 5 4678&lt;BR /&gt;2 6 21253&lt;BR /&gt;3 7 32534&lt;BR /&gt;3 1 23423&lt;BR /&gt;3 2 23567&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want output to look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1 4365&lt;BR /&gt;1 2 457&lt;BR /&gt;1 3 124&lt;BR /&gt;1 4 35547&lt;/P&gt;&lt;P&gt;1 5&amp;nbsp; .&lt;/P&gt;&lt;P&gt;1 6&amp;nbsp; .&lt;/P&gt;&lt;P&gt;1 7&amp;nbsp; .&lt;/P&gt;&lt;P&gt;2 1&amp;nbsp; .&lt;/P&gt;&lt;P&gt;2 2 .&lt;/P&gt;&lt;P&gt;2 3 .&lt;/P&gt;&lt;P&gt;2 4.&lt;BR /&gt;2 5 4678&lt;BR /&gt;2 6 21253&lt;/P&gt;&lt;P&gt;2 7 .&lt;/P&gt;&lt;P&gt;3 1&amp;nbsp;23423&lt;/P&gt;&lt;P&gt;3 2&amp;nbsp;23567&lt;/P&gt;&lt;P&gt;3 3 .&lt;/P&gt;&lt;P&gt;3 4 .&lt;/P&gt;&lt;P&gt;3 5 .&lt;/P&gt;&lt;P&gt;3 6&lt;BR /&gt;3 7 32534&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;So basically i want to increment the values of week to 7 and those which don't have any values in spend should be missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a;&lt;BR /&gt;set spend;&lt;BR /&gt;do j = 1 to 7;&lt;BR /&gt;week = j;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;drop j;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in above code cust id 1 and 3 are getting repeated 2 times and spend values are also not correct.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 15:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570598#M160923</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2019-07-02T15:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570607#M160925</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
run;
%let num_of_weeks=7;
data want ;
if _n_=1 then do;
	if 0 then set spend;
   dcl hash H (dataset: "spend") ;
   h.definekey  ("custid","week") ;
   h.definedata ("spends") ;
   h.definedone () ;
   end;
set spend(keep=custid);
by custid;
if first.custid;
do week=1 to &amp;amp;num_of_weeks;
if h.find() ne 0 then call missing(spends);
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 15:37:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570607#M160925</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-07-02T15:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570688#M160951</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211005"&gt;@adi121&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A very elegant solution - i am always impressed by people who has hash objects as their first choice in the tool box. This is an interesting problem, because it can be solved in so many ways, and I had great fun experimenting with different techniques. Here are two more traditional solutions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
run;

/* first method - requires input sorted by custid */
data want1 (drop= _:); 
	set spend; by custid;
	array w _wk1-_wk7;
	array s _sp1-_sp7;
	retain _wk1-_wk7 _sp1-_sp7;

	* initiate arrays for weeks and spends with missing values for given customer;
	if first.custid then do;
		call missing(of _wk1-_wk7);
		call missing(of _sp1-_sp7);
	end;

	* place actual week/spends in array variables;
	w{week} = week;
	s{week} = spends;

	* output all weeks after all observations for given customer are processed;
	if last.custid then do _i = 1 to 7;
		week = w{_i};
		spends = s{_i};
		output;
	end;
run;

/* second method - requires input sorted by custid and week */
proc sort data=spend; by custid week;
run;

data want2 (drop= last week spends rename=(nweek=week nspends=spends)); 
	set spend; by custid;
	retain last;
	last = lag(week);
	if first.custid then last = 0;

	* add missing weeks before/between weeks in input;
	do i = last + 1 to week - 1;
		nweek = i;
		nspends = .;
		output;
	end;

	* actual observation; 
	nweek = week;
	nspends = spends;
	output;

	* add missing weeks after last week in input;
	if last.custid and week &amp;lt; 7 then do i = week + 1 to 7;
		nweek = i;
		nspends = .;
		output;
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 18:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570688#M160951</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-07-02T18:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570707#M160955</link>
      <description>&lt;P&gt;Here's a less elegant but simple set of steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
run;
proc sort data=spend; by custid week; run; 
proc sql noprint;
	select distinct custid into :custids separated by ","
	from spend;
quit;
/* create empty table*/
data empty (drop=i);
	length custid week 8.;
	do i = 1 to countc("&amp;amp;custids.",",") +1;
		do week=1 to 7;
			custid = scan("&amp;amp;custids.",i,",");
			output;
		end;
	end;
run;
/*populate values into formerly empty table*/
data want;
	merge empty spend;
	by custid week;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 19:01:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570707#M160955</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-07-02T19:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570724#M160962</link>
      <description>&lt;P&gt;Thank you Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here's another one&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
run;

proc freq data=spend(keep=custid week) noprint;
tables custid*week/sparse out=temp(keep=custid week);
run;

proc sort data=spend out=_spend;
by custid week;
run;

data want;
merge temp _spend;
by custid week;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes OP wants the maximum week i.e 7 in one of the by group&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 19:21:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570724#M160962</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-07-02T19:21:52Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570928#M161047</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spend;
input custid week spends;
cards;
1 1 4365
1 2 457
1 3 124
1 4 35547
2 5 4678
2 6 21253
3 7 32534
3 1 23423
3 2 23567
;
proc sql;
create table want as
 select a.*,spends
  from (select * from
(select distinct custid from spend),(select distinct week from spend)
) as a left join spend as b
on a.custid=b.custid and a.week=b.week
order by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jul 2019 11:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570928#M161047</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-03T11:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570986#M161056</link>
      <description>&lt;P&gt;The right solution depends on how to determine the wanted list of weeks.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know the range of weeks is always 1 to 7 then use that to generate the missing records.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data skeleton;
  set spend(keep=custid week);
  by custid;
  if first.custid then do week=1 to 7;
    output;
  end;
run;
data want;
  merge skeleton spend;
  by custid week;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the list is more flexible then you will need to do more work to generate the skeleton dataset.&amp;nbsp; For example you might want to find the maximum values of week.&amp;nbsp; Or perhaps the min and the max.&amp;nbsp; Or just the list of weeks that appear for any custid.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 14:22:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/570986#M161056</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-03T14:22:08Z</dc:date>
    </item>
    <item>
      <title>Re: Incrementing values of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/575159#M162631</link>
      <description>&lt;P&gt;Thanks everyone for quick response. all solutions are working properly.&lt;/P&gt;&lt;P&gt;But if i had to chose one , i would chose the solution given by Tom which was very easy to understand .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks to noling ,Eriklund ,novinsorin&lt;SPAN class="login-bold"&gt;&amp;nbsp;and Ksharp for taking out your precious time.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Jul 2019 18:16:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Incrementing-values-of-variable/m-p/575159#M162631</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2019-07-20T18:16:57Z</dc:date>
    </item>
  </channel>
</rss>

