<?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 Need to create a macro variable from a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672862#M202273</link>
    <description>&lt;P&gt;I have 2 datasets named named Test1 and Test2&amp;nbsp;&lt;/P&gt;&lt;P&gt;Test1 is having no records now and i need to create a macro variable (cat1) from test1 with a value cat1=0 since this has&amp;nbsp; no records , but in future if data comes that macro value should be cat1=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;similarly test2 has data so this macro variable name should be cat2=1 ,&lt;/P&gt;&lt;P&gt;also if no data in future&amp;nbsp; I should get a macro which cat2=0&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like this the code should be dynamic ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jul 2020 14:57:47 GMT</pubDate>
    <dc:creator>ambadi007</dc:creator>
    <dc:date>2020-07-28T14:57:47Z</dc:date>
    <item>
      <title>Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672862#M202273</link>
      <description>&lt;P&gt;I have 2 datasets named named Test1 and Test2&amp;nbsp;&lt;/P&gt;&lt;P&gt;Test1 is having no records now and i need to create a macro variable (cat1) from test1 with a value cat1=0 since this has&amp;nbsp; no records , but in future if data comes that macro value should be cat1=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;similarly test2 has data so this macro variable name should be cat2=1 ,&lt;/P&gt;&lt;P&gt;also if no data in future&amp;nbsp; I should get a macro which cat2=0&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like this the code should be dynamic ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 14:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672862#M202273</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-07-28T14:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672867#M202274</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(index);
%let dsid=%sysfunc(open(test&amp;amp;index));
    
 %if &amp;amp;dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&amp;amp;dsid,nlobs));
  %let rc=%sysfunc(close(&amp;amp;dsid));
   %if &amp;amp;cnt ne 0 %then %do;
    %let var&amp;amp;index = 1;
   %end;
   %else %do;
        %let var&amp;amp;index = 0;
   %end;
 %end;
 %else %put test&amp;amp;index cannot be open.;
%mend drive;

%drive(1)
%drive(2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested and modified from here - which has full details of the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have 2 datasets named named Test1 and Test2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test1 is having no records now and i need to create a macro variable (cat1) from test1 with a value cat1=0 since this has&amp;nbsp; no records , but in future if data comes that macro value should be cat1=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;similarly test2 has data so this macro variable name should be cat2=1 ,&lt;/P&gt;
&lt;P&gt;also if no data in future&amp;nbsp; I should get a macro which cat2=0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;like this the code should be dynamic ...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help appreciated ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672867#M202274</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-28T15:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672871#M202275</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro create_cat(dsnumber);
%global cat&amp;amp;dsnumber.;
%let cat&amp;amp;dsnumber. = 0;
%if %sysfunc(exist(test&amp;amp;dsnumber.))
%then %do;
  data _null_;
  set test&amp;amp;dsnumber.;
  call symputx("cat&amp;amp;dsnumber.","1");
  stop;
  run;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the dataset has no observations, the data step terminates before it reaches the CALL SYMPUTX. Otherwise it reads just one observation.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672871#M202275</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-28T15:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672872#M202276</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have 2 datasets named named Test1 and Test2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test1 is having no records now and i need to create a macro variable (cat1) from test1 with a value cat1=0 since this has&amp;nbsp; no records , but in future if data comes that macro value should be cat1=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;similarly test2 has data so this macro variable name should be cat2=1 ,&lt;/P&gt;
&lt;P&gt;also if no data in future&amp;nbsp; I should get a macro which cat2=0&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select count(*)&amp;gt;0 into :cat1 from test1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;like this the code should be dynamic ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;dynamic with respect to what? What is changing that would require dynamic code?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:12:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672872#M202276</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-28T15:12:24Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672874#M202277</link>
      <description>&lt;P&gt;Just double-checking, since the solutions would be quite different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have clearly stated that you are looking to see if the data set exists, but has zero observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible that you really need to check for whether the data set doesn't exist at all?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672874#M202277</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-28T15:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672875#M202278</link>
      <description>&lt;P&gt;A simple data step will do that.&amp;nbsp; Make sure to code in a way that works when there are zero observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let cnt1=0;
data _null_;
  set test1;
  call symputx('cnt1','1');
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672875#M202278</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-28T15:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672880#M202281</link>
      <description>&lt;P&gt;I tried the code but Iam getting if the data is available i.e the&amp;nbsp;&lt;SPAN&gt;cat1=1 but if no records Iam not getting the zero value in the macro variable&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(index);
%let dsid=%sysfunc(open(test&amp;amp;index));
    
 %if &amp;amp;dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&amp;amp;dsid,nlobs));
  %let rc=%sysfunc(close(&amp;amp;dsid));
   %if &amp;amp;cnt ne 0 %then %do;
    %let var&amp;amp;index = 1;
   %end;
   %else %do;
        %let var&amp;amp;index = 0;
   %end;
 %end;
 %else %put test&amp;amp;index cannot be open.;
%mend drive;

%drive(1)
%drive(2)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Untested and modified from here - which has full details of the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I have 2 datasets named named Test1 and Test2&amp;nbsp;&lt;/P&gt;&lt;P&gt;Test1 is having no records now and i need to create a macro variable (cat1) from test1 with a value cat1=0 since this has&amp;nbsp; no records , but in future if data comes that macro value should be cat1=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;similarly test2 has data so this macro variable name should be cat2=1 ,&lt;/P&gt;&lt;P&gt;also if no data in future&amp;nbsp; I should get a macro which cat2=0&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like this the code should be dynamic ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672880#M202281</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-07-28T15:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672887#M202286</link>
      <description>&lt;P&gt;I just need two macro variable if the dateset is empty then cat1=0 else cat1=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like wise for another dataset if no records cat2=0 else cat2=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;because i need to use these macros variable later in my code&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:32:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672887#M202286</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-07-28T15:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672899#M202292</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I just need two macro variable if the dateset is empty then cat1=0 else cat1=1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;like wise for another dataset if no records cat2=0 else cat2=1&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So now you say you just need these two macro variables, and not others? Many samples of code have been provided, I'm sure some of them must work (like the code I provided). Have you tried them?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672899#M202292</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-28T15:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672902#M202295</link>
      <description>&lt;P&gt;it worrked which has data ...&amp;nbsp; but for getting cat1=0 that time it is not working&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672902#M202295</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-07-28T15:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672903#M202296</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(index);

%global var&amp;amp;index;

%let dsid=%sysfunc(open(test&amp;amp;index));
    
 %if &amp;amp;dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&amp;amp;dsid,nlobs));
  %let rc=%sysfunc(close(&amp;amp;dsid));
   %if &amp;amp;cnt ne 0 %then %do;
    %let var&amp;amp;index = 1;
   %end;
   %else %do;
        %let var&amp;amp;index = 0;
   %end;
 %end;
 %else %let var&amp;amp;index = 0;;
%mend drive;

%drive(1)
%drive(2)

%put var1.;
%put var2.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;FYI - most likely this would be easier to do at the end of whatever other step is creating the multiple data sets. What makes you think you need an approach like this by the way? It's almost never needed IME, usually even a WHERE is preferable for dynamic code for simplicity.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672903#M202296</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-28T15:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a macro variable from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672904#M202297</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;it worrked which has data ...&amp;nbsp; but for getting cat1=0 that time it is not working&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There are many solutions provided. Which one are you talking about? Why don't you try them all?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-macro-variable-from-a-dataset/m-p/672904#M202297</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-28T15:58:18Z</dc:date>
    </item>
  </channel>
</rss>

