<?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 combine datasets even when results are missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912383#M359680</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* have COURSES-dataset */
data have_course;
    input PLAN COURSE_ID $;
    datalines;
1001 CRS_101
1001 CRS_102
1001 CRS_103
1002 CRS_201
1002 CRS_202
1002 CRS_203
1003 CRS_101
1003 CRS_301
1003 CRS_302
1003 CRS_303
;

/* have STUDENT-dataset */
data have_student;
    input ID $ COURSE_ID $ RESULT PLAN;
    datalines;
001 CRS_101 7 1001
001 CRS_102 6 1001
001 CRS_103 5 1001
001 CRS_103 6 1001
001 CRS_201 7 1002
001 CRS_202 3 1002
001 CRS_202 5 1002
001 CRS_203 6 1002
002 CRS_101 8 1001
002 CRS_102 7 1001
002 CRS_103 6 1001
002 CRS_201 9 1002
002 CRS_202 8 1002
002 CRS_203 4 1002
002 CRS_203 5 1002
003 CRS_103 6 1001
003 CRS_201 9 1002
;

/* Sort COURES-dataset on PLAN */
proc sort data=have_course;
    by PLAN COURSE_ID;
run;

/* Sort STUDENT-dataset on PLAN */
proc sort data=have_student;
    by PLAN COURSE_ID ID;
run;

/* create course list datasets based on plan */
data _null_;
    set have_course;
    by PLAN COURSE_ID;
    call execute(cats(
        'data CRS_SPL_', plan, '; ',
        'set have_course;',
        'if plan = ', plan, ';',
        'run;'
    ));
run;


/* what i want */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want in each (seperated) dataset, which is called/begins with: CRS_SPL_* :&lt;BR /&gt;-I want every student which had a result for that plan in it&lt;BR /&gt;with every crs_* listed even if that student had no result for that course.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I hope I made it clear..if not, i hope the screenshot of what I want, clears this very much. &lt;BR /&gt;If not, please ask. I will try to clarify this more than. Thank you very much in advance!&lt;/P&gt;
&lt;P&gt;So in Excel it will look like this, exactly what I want to achieve in SAS:&lt;BR /&gt;see attached picture&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Want.png" style="width: 408px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92841iECC07A73A672437B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Want.png" alt="Want.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please do ignore the coloring ..i colored the tables just to highlight the similarities/differences. Thank you!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 21 Jan 2024 15:55:09 GMT</pubDate>
    <dc:creator>SAS_Question</dc:creator>
    <dc:date>2024-01-21T15:55:09Z</dc:date>
    <item>
      <title>combine datasets even when results are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912383#M359680</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* have COURSES-dataset */
data have_course;
    input PLAN COURSE_ID $;
    datalines;
1001 CRS_101
1001 CRS_102
1001 CRS_103
1002 CRS_201
1002 CRS_202
1002 CRS_203
1003 CRS_101
1003 CRS_301
1003 CRS_302
1003 CRS_303
;

/* have STUDENT-dataset */
data have_student;
    input ID $ COURSE_ID $ RESULT PLAN;
    datalines;
001 CRS_101 7 1001
001 CRS_102 6 1001
001 CRS_103 5 1001
001 CRS_103 6 1001
001 CRS_201 7 1002
001 CRS_202 3 1002
001 CRS_202 5 1002
001 CRS_203 6 1002
002 CRS_101 8 1001
002 CRS_102 7 1001
002 CRS_103 6 1001
002 CRS_201 9 1002
002 CRS_202 8 1002
002 CRS_203 4 1002
002 CRS_203 5 1002
003 CRS_103 6 1001
003 CRS_201 9 1002
;

/* Sort COURES-dataset on PLAN */
proc sort data=have_course;
    by PLAN COURSE_ID;
run;

/* Sort STUDENT-dataset on PLAN */
proc sort data=have_student;
    by PLAN COURSE_ID ID;
run;

/* create course list datasets based on plan */
data _null_;
    set have_course;
    by PLAN COURSE_ID;
    call execute(cats(
        'data CRS_SPL_', plan, '; ',
        'set have_course;',
        'if plan = ', plan, ';',
        'run;'
    ));
run;


/* what i want */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want in each (seperated) dataset, which is called/begins with: CRS_SPL_* :&lt;BR /&gt;-I want every student which had a result for that plan in it&lt;BR /&gt;with every crs_* listed even if that student had no result for that course.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I hope I made it clear..if not, i hope the screenshot of what I want, clears this very much. &lt;BR /&gt;If not, please ask. I will try to clarify this more than. Thank you very much in advance!&lt;/P&gt;
&lt;P&gt;So in Excel it will look like this, exactly what I want to achieve in SAS:&lt;BR /&gt;see attached picture&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Want.png" style="width: 408px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92841iECC07A73A672437B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Want.png" alt="Want.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please do ignore the coloring ..i colored the tables just to highlight the similarities/differences. Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2024 15:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912383#M359680</guid>
      <dc:creator>SAS_Question</dc:creator>
      <dc:date>2024-01-21T15:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: combine datasets even when results are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912384#M359681</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195211"&gt;@SAS_Question&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code creates the wanted output. I must admit that I could not follow your line of reasoning behind the data _null_ step, so I made it "my own way".&amp;nbsp; The code should be able to handle more complex input, like more results to put in different columns.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* have COURSES-dataset */
data have_course;
    input PLAN COURSE_ID $;
    datalines;
1001 CRS_101
1001 CRS_102
1001 CRS_103
1002 CRS_201
1002 CRS_202
1002 CRS_203
1003 CRS_101
1003 CRS_301
1003 CRS_302
1003 CRS_303
;

/* have STUDENT-dataset */
data have_student;
    input ID $ COURSE_ID $ RESULT PLAN;
    datalines;
001 CRS_101 7 1001
001 CRS_102 6 1001
001 CRS_103 5 1001
001 CRS_103 6 1001
001 CRS_201 7 1002
001 CRS_202 3 1002
001 CRS_202 5 1002
001 CRS_203 6 1002
002 CRS_101 8 1001
002 CRS_102 7 1001
002 CRS_103 6 1001
002 CRS_201 9 1002
002 CRS_202 8 1002
002 CRS_203 4 1002
002 CRS_203 5 1002
003 CRS_103 6 1001
003 CRS_201 9 1002
;
/* PREPARE INPUT */
* Multiply records in have_course by ID;
proc sql;
  create table plan_exp as
    select a.PLAN, a.COURSE_ID, b.ID
    from have_course as a, (select distinct ID from have_student) as b
    order by a.PLAN, b.ID, a.COURSE_ID;
quit;

* Sort STUDENT-dataset on PLAN;
proc sort data=have_student;
    by PLAN ID COURSE_ID;
run;

* Merge to get all plans + results in same dataset;
data w1;
  merge plan_exp have_student;
  by PLAN ID COURSE_ID;
run;

* Transpose have_student - results in columns instead of rows;
proc transpose data=w1 prefix=RESULT_ out=w2 (drop=_NAME_);
  by PLAN ID COURSE_ID;
run;

/* CREATE OUTPUT */
* Build list and count of plans;
proc sql noprint;
  select distinct cats('CRS_SPL_', PLAN) 
  into :dslist separated by '/'
  from w2;
quit;
%put &amp;amp;=dslist;
%put &amp;amp;=sqlobs;

* Split in separate datasets;
* Note the length statement before the set statement is to bring variables in correct order;
%macro m;
  %do i = 1 %to &amp;amp;sqlobs;
    %let thisds = %scan(&amp;amp;dslist,&amp;amp;i,/);
    %let thisplan = %scan(&amp;amp;thisds,3,_);
    data &amp;amp;thisds;
      length PLAN 8 COURSE_ID $8 ID $8;
      set w2 (where=(PLAN=input("&amp;amp;thisplan",8.)));
    run;
  %end;
%mend;
%m;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2024 17:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912384#M359681</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2024-01-21T17:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: combine datasets even when results are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912385#M359682</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195211"&gt;@SAS_Question&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code creates the wanted output. I must admit that I could not follow your line of reasoning behind the data _null_ step, so I made it "my own way".&amp;nbsp; The code should be able to handle more complex input, like more results to put in different columns.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Firstly, thank you very much for this simple solution and very quick response! Thank you very much! &lt;BR /&gt;&lt;BR /&gt;As for your remark ("I must admit that I could not follow your line of reasoning behind the data _null_ step"), dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;, I was going the wrong way with my code and completely blocked ... but this way of your code is neat and quick of you... saved my day!&amp;nbsp;&lt;BR /&gt;I really like the way you did that with PROC SQL!&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2024 17:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912385#M359682</guid>
      <dc:creator>SAS_Question</dc:creator>
      <dc:date>2024-01-21T17:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: combine datasets even when results are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912386#M359683</link>
      <description>&lt;P&gt;This statement seems confused.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set w2 (where=(PLAN=input("&amp;amp;thisplan",8.)));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If THISPLAN has a string that looks like a number you can just do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set w2 (where=(PLAN=&amp;amp;thisplan));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if it doesn't then PLAN should be a character variable so you should use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set w2 (where=(PLAN="&amp;amp;thisplan"));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note also that if THISPLAN is a number that is more than 8 digits long using the 8. informat will only read the first 8 digits.&amp;nbsp; If you you want to use the INPUT() function the just use the maximum width of 32.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2024 18:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912386#M359683</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-21T18:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: combine datasets even when results are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912422#M359692</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195211"&gt;@SAS_Question&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are right, of course.&amp;nbsp; I used double quotes because I thought PLAN was a character variable, and then just added the input function to get it to work instead of removing the quotes. Not exactly elegant.&amp;nbsp;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2024 08:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combine-datasets-even-when-results-are-missing/m-p/912422#M359692</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2024-01-22T08:17:26Z</dc:date>
    </item>
  </channel>
</rss>

