<?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 append in loop in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530609#M5773</link>
    <description>&lt;P&gt;Create a lookup table with a cartesian join, then use that in a left join, and do a case when for missing values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
input year;
cards;
2012
2013
2014
;
run;

data data2;
input state $ year value;
cards;
A 2012 4
A 2013 6
B 2013 10
;
run;

proc sql;
create table all_states as
select distinct
  a.year,
  b.state
from
  data1 a,
  data2 b
;
create table want as
select
  a.year,
  a.state,
  case
    when b.value is missing then 0
    else b.value
  end as value
from
  all_states a left join data2 b
  on a.year = b.year and a.state = b.state
order by
  a.state,
  a.year
;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;year    state    value

2012      A         4 
2013      A         6 
2014      A         0 
2012      B         0 
2013      B        10 
2014      B         0 
&lt;/PRE&gt;
&lt;P&gt;Note how I present example data in data steps with datalines, for easy use by others (just a copy/paste and submit is needed to reliably recreate the dataset). See it as a basic courtesy for potential helpers.&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jan 2019 13:07:28 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-01-28T13:07:28Z</dc:date>
    <item>
      <title>SAS append in loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530600#M5767</link>
      <description>Thanks for your comments. Here I create a very simple example for the above. Inputs are&lt;BR /&gt;&lt;BR /&gt;Data 1:&lt;BR /&gt;Year&lt;BR /&gt;2012&lt;BR /&gt;2013&lt;BR /&gt;2014&lt;BR /&gt;&lt;BR /&gt;Data2:&lt;BR /&gt;State year value&lt;BR /&gt;A. 2012. 4&lt;BR /&gt;A. 2013. 6&lt;BR /&gt;B. 2013. 10&lt;BR /&gt;&lt;BR /&gt;Output:&lt;BR /&gt;State. Year. Value&lt;BR /&gt;A. 2012. 4&lt;BR /&gt;A. 2013. 6&lt;BR /&gt;A. 2014. 0&lt;BR /&gt;B. 2012. 0&lt;BR /&gt;B. 2013. 10&lt;BR /&gt;B. 2014. 0&lt;BR /&gt;&lt;BR /&gt;We might have a number of states which might not be possible to execute one by one manually.</description>
      <pubDate>Mon, 28 Jan 2019 12:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530600#M5767</guid>
      <dc:creator>Souradip</dc:creator>
      <dc:date>2019-01-28T12:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS append in loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530609#M5773</link>
      <description>&lt;P&gt;Create a lookup table with a cartesian join, then use that in a left join, and do a case when for missing values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
input year;
cards;
2012
2013
2014
;
run;

data data2;
input state $ year value;
cards;
A 2012 4
A 2013 6
B 2013 10
;
run;

proc sql;
create table all_states as
select distinct
  a.year,
  b.state
from
  data1 a,
  data2 b
;
create table want as
select
  a.year,
  a.state,
  case
    when b.value is missing then 0
    else b.value
  end as value
from
  all_states a left join data2 b
  on a.year = b.year and a.state = b.state
order by
  a.state,
  a.year
;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;year    state    value

2012      A         4 
2013      A         6 
2014      A         0 
2012      B         0 
2013      B        10 
2014      B         0 
&lt;/PRE&gt;
&lt;P&gt;Note how I present example data in data steps with datalines, for easy use by others (just a copy/paste and submit is needed to reliably recreate the dataset). See it as a basic courtesy for potential helpers.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 13:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530609#M5773</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-28T13:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS append in loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530645#M5782</link>
      <description>Thanks a lot for the solution.</description>
      <pubDate>Mon, 28 Jan 2019 15:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-append-in-loop/m-p/530645#M5782</guid>
      <dc:creator>Souradip</dc:creator>
      <dc:date>2019-01-28T15:11:22Z</dc:date>
    </item>
  </channel>
</rss>

