<?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: Merge a dataset with a list in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988507#M43908</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; And, in the interest of alternatives, if you only need a report, you could use PROC REPORT. And, if you do need a new dataset, then PROC REPORT has an OUT= option that would work. The trick is to create a format that has all of your desired row values, in the order you want them to appear. Then,&amp;nbsp; PROC REPORT supports the PRELOADFMT option and if you use that option coupled with the COMPLETEROWS option, then PROC&amp;nbsp; REPORT will automatically make your report with the "extra" rows inserted where you want them. By default your missing numeric columns would be represented by . for missing values, but you can use the MISSING=0&amp;nbsp; option to take care of showing 0 instead of . on the report (and in the new dataset if you use the OUT= option).&lt;/P&gt;&lt;P&gt;&amp;nbsp; For a small list and small starting dataset, this can be a simpler technique, especially if you really only need a report.&lt;/P&gt;&lt;P&gt;Cynthia&lt;/P&gt;&lt;P&gt;Example code: (BTW, I typically avoid using N as a column name, just because I don't want to possibly get confused in other procs where I need to use the N statistic)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value $list 'A'='A'
              'B'='B'
              'C'='C'
              'D'='D'
              'E'='E'
              'F'='F';
run;

data DB;
  infile datalines;
  input test $ count;
  datalines;
A 3
C 8
F 2
;
run;

proc print data=DB;
  title '1) Original file';
run;

options missing=0;
proc report data=DB completerows out=newDB(drop=_BREAK_);
  title '2) PROC REPORT step makes a report with count=0 for extra rows';
  title2 'and creates new dataset called work.newDB';
  title3 'Obs in newDB are in the order of the formatted values';
  column test count;
  define test / group f=$list. preloadfmt;
  define count / sum;
run;

proc print data=newDB;
title '3) After PROC REPORT -- new dataset newDB showing extra rows with 0';
title2 'If only report is needed, can use PROC REPORT output and remove OUT= option';
title3 'so work.newDB is not created';
run;

title;
options missing=.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 May 2026 16:32:40 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2026-05-21T16:32:40Z</dc:date>
    <item>
      <title>Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988462#M43902</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input Test :$200. N;
cards;
A  3
C  8
F  2
run;

list = A, B, C, D, E, F&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to get the following?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input Test :$200. N;
cards;
A  3
B  0
C  8
D  0
E  0
F  2
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words I would like to merge my DB with the elements in the list whose value will be 0 when not in DB and keeping the order of list.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2026 13:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988462#M43902</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-21T13:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988466#M43903</link>
      <description>&lt;P&gt;There is not a list concept/object in SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you make a dataset called LIST with variable TEST and records with values "A" through "F", you can then merge that with your DB dataset, and use the&amp;nbsp; in= option set values to 0 when they are not in your DB dataset.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2026 14:03:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988466#M43903</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2026-05-21T14:03:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988467#M43904</link>
      <description>Thank you very much. Is there an example?</description>
      <pubDate>Thu, 21 May 2026 14:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988467#M43904</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-21T14:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988469#M43905</link>
      <description>&lt;P&gt;Use the "list" to make a dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example with a DO statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
  do test='A','B', 'C', 'D', 'E', 'F' ;
     N=0;
     output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or&amp;nbsp;just put the list into in-line data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
  infile cards dsd ;
  input test :$200. @@ ;
  N=0;
cards;
A, B, C, D, E, F
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can MERGE.&amp;nbsp; Place the real dataset last in the MERGE statement so its values of N (and other variables) "wins".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge list have ;
  by test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2026 14:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988469#M43905</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-21T14:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988474#M43906</link>
      <description>&lt;P&gt;If your list is not extremely long you can try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input Test :$20. N;
cards;
A  3
C  8
F  2
;
run;
proc print data=DB; /* preview data */
run;


%let list = A, B, C, D, E, F; /* your comma separated list up to 32767 bytes long */

data WANT;
  if 0 then set DB;
  declare hash H(dataset:"DB");
  H.defineKey('Test');
  H.defineData(ALL:'Y');
  H.defineDone();

  length list $ 32767;
  list = symget("list");
  do until (end);
    i+1;
    Test = strip(scan(list, i, ","));
    if test = " " then end=1;
    else 
      do;
        if H.find() then N=0;
        output;
      end;
  end;
  keep Test N;
run;


proc print data=WANT; /* preview result */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2026 14:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988474#M43906</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2026-05-21T14:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988506#M43907</link>
      <description>Thank you very much for your help. It gives the following: "ERROR: BY variables are not properly sorted on data set WORK.LIST." I want exactly the order in "list". In real data it is not alphabetical order.</description>
      <pubDate>Thu, 21 May 2026 16:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988506#M43907</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-21T16:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988507#M43908</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; And, in the interest of alternatives, if you only need a report, you could use PROC REPORT. And, if you do need a new dataset, then PROC REPORT has an OUT= option that would work. The trick is to create a format that has all of your desired row values, in the order you want them to appear. Then,&amp;nbsp; PROC REPORT supports the PRELOADFMT option and if you use that option coupled with the COMPLETEROWS option, then PROC&amp;nbsp; REPORT will automatically make your report with the "extra" rows inserted where you want them. By default your missing numeric columns would be represented by . for missing values, but you can use the MISSING=0&amp;nbsp; option to take care of showing 0 instead of . on the report (and in the new dataset if you use the OUT= option).&lt;/P&gt;&lt;P&gt;&amp;nbsp; For a small list and small starting dataset, this can be a simpler technique, especially if you really only need a report.&lt;/P&gt;&lt;P&gt;Cynthia&lt;/P&gt;&lt;P&gt;Example code: (BTW, I typically avoid using N as a column name, just because I don't want to possibly get confused in other procs where I need to use the N statistic)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value $list 'A'='A'
              'B'='B'
              'C'='C'
              'D'='D'
              'E'='E'
              'F'='F';
run;

data DB;
  infile datalines;
  input test $ count;
  datalines;
A 3
C 8
F 2
;
run;

proc print data=DB;
  title '1) Original file';
run;

options missing=0;
proc report data=DB completerows out=newDB(drop=_BREAK_);
  title '2) PROC REPORT step makes a report with count=0 for extra rows';
  title2 'and creates new dataset called work.newDB';
  title3 'Obs in newDB are in the order of the formatted values';
  column test count;
  define test / group f=$list. preloadfmt;
  define count / sum;
run;

proc print data=newDB;
title '3) After PROC REPORT -- new dataset newDB showing extra rows with 0';
title2 'If only report is needed, can use PROC REPORT output and remove OUT= option';
title3 'so work.newDB is not created';
run;

title;
options missing=.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 May 2026 16:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988507#M43908</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2026-05-21T16:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge a dataset with a list</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988508#M43909</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you very much for your help. It gives the following: "ERROR: BY variables are not properly sorted on data set WORK.LIST." I want exactly the order in "list". In real data it is not alphabetical order.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case you probably want to make sub-list that only includes the MISSING observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table sublist as
  select *
  from list 
  where test not in (select test from have)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can then append instead of merge with the original dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have sublist;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there an an ORDER you do want to preserve then you might need to make a NEW variable that does properly define the order you want.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2026 16:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merge-a-dataset-with-a-list/m-p/988508#M43909</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-21T16:38:52Z</dc:date>
    </item>
  </channel>
</rss>

