<?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: Create new SAS dataset based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305965#M270513</link>
    <description>&lt;P&gt;Yes, it would be part of the original dataset if an observation existed.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Oct 2016 15:00:12 GMT</pubDate>
    <dc:creator>elwayfan446</dc:creator>
    <dc:date>2016-10-20T15:00:12Z</dc:date>
    <item>
      <title>Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305802#M270509</link>
      <description>&lt;P&gt;I am trying to figure out a way to create a new SAS dataset with an if/then statement or case statement in proc sql.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am looking at is a dataset that doesn't have any observations in it at all.&amp;nbsp; I have some code that will give me a zero in a temp dataset if no observations are found in the original set.&amp;nbsp; What I need to be able to do is either:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; Create a dataset with 3 observations where the first variable is populated with something I hard code and the rest of the variables 0's if the main dataset has no observations or the temp dataset is showing 0 indicating there were no observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; If there are observations in the main dataset, then I want to create the new dataset based off of those observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have researched but haven't really found a way yet.&amp;nbsp; At least not one that is recognizable.&amp;nbsp; I found this paper but I am not sure if it would help me here.&amp;nbsp; &lt;A href="http://www.lexjansen.com/phuse/2014/cc/CC06.pdf" target="_blank"&gt;http://www.lexjansen.com/phuse/2014/cc/CC06.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 21:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305802#M270509</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-10-19T21:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305816#M270510</link>
      <description>&lt;P&gt;I think this will get you there. &amp;nbsp;One detail: &amp;nbsp;is the variable you hard-code part of the main data set or not? &amp;nbsp;It might make a difference in adding a DROP statement or in setting a length for that variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;if 0 then&amp;nbsp;set have end=done;&lt;/P&gt;
&lt;P&gt;if done=0 then do until (done2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have end=done2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;array nums {*} _numeric_;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do _n_=1 to dim(nums);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; nums{_n_}=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;something_i_hard_code='Some Value';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do _n_=1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 23:03:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305816#M270510</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-19T23:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305837#M270511</link>
      <description>&lt;P&gt;With SQL something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_with_obs;
  var1='a'; var2='b'; var3=1; var4=2;
  do i=1 to 5;
    output;
  end;
run;

data have_zero_obs;
  stop;
  set have_with_obs;
run;

data filler;
  var1='0'; var2='0';
  do i=1 to 3;
    output;
  end;
  stop;
  set have;
run;


proc sql;
  create table want_hasObs as
    select *
    from have_with_obs
    union all corr
    select *
    from filler
    where (select count(*) from have_with_obs)&amp;lt;1
  ;
quit;

proc sql;
  create table want_Filler as
    select *
    from have_zero_obs
    union all corr
    select *
    from filler
    where (select count(*) from have_zero_obs)&amp;lt;1
  ;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead of the "select(*)...." which adds a full pass through the table, you could also query dictionary.tables and retrieve the number of observations from there.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 01:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305837#M270511</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-20T01:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305876#M270512</link>
      <description>&lt;P&gt;Well, the simplest method I can think is a process thing:&lt;/P&gt;
&lt;P&gt;1) create an empty dataset containing the variables you expect in the output&lt;/P&gt;
&lt;P&gt;2) run your program and set the output from that program with the template from step 1&lt;/P&gt;
&lt;P&gt;3) if there are no observations in that dataset then run some code to populate with default text&lt;/P&gt;
&lt;P&gt;So as an example:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT 
  ( 
    A char(200),
    B num,
    C num
  );
quit;

/* run your code */

data want;
  set want results;
run;

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="WANT" and nobs=0));
  /* If nothing is found in the where, then this code will never get called */
  call execute('proc sql; insert into WANT set A="No obs",B=0,C=0; quit;');
run;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Oct 2016 08:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305876#M270512</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-20T08:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305965#M270513</link>
      <description>&lt;P&gt;Yes, it would be part of the original dataset if an observation existed.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 15:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305965#M270513</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-10-20T15:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305980#M270514</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp; Thanks for the help.&amp;nbsp; I am trying to get this to work because it is the answer in this thread that I feel more comfortable with what is going on.&amp;nbsp; However, I have a couple of questions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, I can't figure out which piece of this code will populate my new dataset with data from the orignal dataset if there are observations in it.&amp;nbsp; Also, I can't determine what the "results" set is supposed to represent your example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the flow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Original Dataset -&amp;gt; Either has observations or doesn't -&amp;gt; If it has observations, I am summing totals from the observations and inserting them into a new dataset. -&amp;gt;&amp;nbsp;If it doesn't have observations, I want to insert the records for 0 amounts into the summary dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that makes sense.&amp;nbsp; I feel like this is close to what I need but I am missing something.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 15:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305980#M270514</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-10-20T15:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305996#M270515</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ok, so let me explain.&amp;nbsp; Basically we are using basic datastep and logic to generate strings which are inserted into the compilation phase after the datastep has finished - this is what call execute does.&amp;nbsp; So to break down:&lt;/P&gt;
&lt;PRE&gt;/* Create empty results table */
proc sql;
  create table RESULTS (A num,B num);
quit;

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="ABC"));
/* The above takes SAS metadata about table work.abc */
/* Based on that if there are zero observations from work.abc we send */
/* this text out at the end of the step */
  if nobs=0 then do;
    call execute('proc sql; insert into RESULTS set A=0,B=0; quit;');
  end;
/* This text is called if there are observations */
  else do;
    call execute('proc sql; insert into RESULTS (select sum(VARA) as A,SUM(VARB) as B from WORK.ABC; quit;');
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Now I have overcomplicated it a bit to show the method.&amp;nbsp; Basically if from the metadata zero observations are found in WORK.ABC - which we will assume is your data, then the text string:&lt;/P&gt;
&lt;PRE&gt;proc sql; insert into RESULTS set A=0,B=0; quit;&lt;/PRE&gt;
&lt;P&gt;Gets pushed out to the compiler and that is executed.&amp;nbsp; Otherwise the sum sql statement is pushed out and executed.&lt;/P&gt;
&lt;P&gt;If you want exact code, post some test data in the form of a datastep.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 16:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/305996#M270515</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-20T16:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306004#M270516</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp; ok, I think I am with you.&amp;nbsp; I believe I have that working, however, I am wondering if I am able to do select statements with unions in order to get 3 seperate records inserted into the new dataset if there are no records in the original?&amp;nbsp; Otherwise, is there a way to modify the set statement example you gave me?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 17:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306004#M270516</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-10-20T17:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306040#M270517</link>
      <description>&lt;P&gt;I was able to do what I needed by using 3 seperate insert statements to get my 3 observations.&amp;nbsp; It seems like there would be a more efficient way.&amp;nbsp; If you have any suggestions &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;let me know.&amp;nbsp; At least this is working.&amp;nbsp; I really appreciate your help!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 18:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306040#M270517</guid>
      <dc:creator>elwayfan446</dc:creator>
      <dc:date>2016-10-20T18:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create new SAS dataset based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306207#M270518</link>
      <description>&lt;P&gt;No probs. &amp;nbsp;If you want to post your code I will try to take a look.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 08:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-SAS-dataset-based-on-conditions/m-p/306207#M270518</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-21T08:29:56Z</dc:date>
    </item>
  </channel>
</rss>

