<?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 a function to exclude date and selected numeric variables using dictionary.colums in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-function-to-exclude-date-and-selected-numeric-variables/m-p/525792#M143082</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113781"&gt;@teja04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi all,&lt;BR /&gt;I have two dates with same variables. Before merging these two datasets for calculation. I want to exclude selected numeric variables and all data variables.&lt;BR /&gt;Below is my code:&lt;BR /&gt;data a;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1 2 3&lt;BR /&gt;ch4 ch5 funded 4 5 6&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data b;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7 8 9&lt;BR /&gt;ch4 ch5 total 10 11 12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data c;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1&lt;BR /&gt;ch4 ch5 funded 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data d;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7&lt;BR /&gt;ch4 ch5 total 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%macro caldiff(ds1=, ds2=, fundvar=);&lt;BR /&gt;options mprint mlogic symbolgen;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct name into: charvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='CHAR' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct name into: numvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct strip(name)||"_N" into: newnumvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds2(drop=&amp;amp;numvars i);&lt;BR /&gt;set &amp;amp;ds2;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;do i=1 to dim(old);&lt;BR /&gt;new{i} = old{i};&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds1._new(drop=&amp;amp;newnumvars j);&lt;BR /&gt;merge &amp;amp;ds1(in=fund) &amp;amp;ds2(in=tot);&lt;BR /&gt;by &amp;amp;charvars;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;&lt;BR /&gt;if fund and tot then do;&lt;BR /&gt;do j=1 to dim(old);&lt;BR /&gt;old{j} = new{j} - old{j};&lt;BR /&gt;end;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend caldiff;&lt;BR /&gt;%caldiff(ds1=c, ds2=d, fundvar=c3);&lt;BR /&gt;%caldiff(ds1=a, ds2=b, fundvar=c3);&lt;BR /&gt;&lt;BR /&gt;For Example, I want to exclude N1 variable from calculation. Could someone please help me in issue.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please describe the rule for how to know that N1 is a variable to exclude.&lt;/P&gt;
&lt;P&gt;Or are you looking for a way to provide a list of values instead to use? If that is the case then perhaps you are looking for something like this:&lt;/P&gt;
&lt;PRE&gt;%macro caldiff(ds1=, ds2=, fundvar=);
   options mprint mlogic symbolgen;
   %let ds1 =%upcase(&amp;amp;ds1.);
   %let ds2 =%upcase(&amp;amp;ds2.);
   %let temp =%upcase(&amp;amp;fundvar.);
   %let fundvar=;
   /* this do loop creates a quoted list*/
   %do i=1 %to %sysfunc(countw(&amp;amp;temp));
      %let fundvar = &amp;amp;fundvar. %sysfunc(quote(%scan(&amp;amp;temp.,&amp;amp;i.)));
   %end;

   proc sql;
      select distinct name into: charvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='CHAR' and 
            upcase(name) not in ( &amp;amp;fundvar.);
      select distinct name into: numvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='NUM' and 
            upcase(name) not in (&amp;amp;fundvar.);
      select distinct strip(name)||"_N" into: newnumvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='NUM' and 
            upcase(name)  not in ( &amp;amp;fundvar.);
   quit;

/*other code*/
%mend;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might suggest that your code might look a little cleaner by %upcase the macro variables before the first use so the remaining is a bit easier to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also use of this construct:&lt;/P&gt;
&lt;PRE&gt;data &amp;amp;ds2(drop=&amp;amp;numvars i);
set &amp;amp;ds2;&lt;/PRE&gt;
&lt;P&gt;is extremely dangerous, especially while testing code as when the input and output set are the same as the original data set is completely replaced and determining where a code logic problem occurred may be extremely difficult.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note the use of the forum {I} icon to open a code box for pasting code. The main message windows will reformat text removing indents and such with resulting code hard to read.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Jan 2019 17:50:31 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-01-09T17:50:31Z</dc:date>
    <item>
      <title>Create a function to exclude date and selected numeric variables using dictionary.colums</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-function-to-exclude-date-and-selected-numeric-variables/m-p/525777#M143076</link>
      <description>Hi all,&lt;BR /&gt;I have two dates with same variables. Before merging these two datasets for calculation. I want to exclude selected numeric variables and all data variables.&lt;BR /&gt;Below is my code:&lt;BR /&gt;data a;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1 2 3&lt;BR /&gt;ch4 ch5 funded 4 5 6&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data b;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7 8 9&lt;BR /&gt;ch4 ch5 total 10 11 12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data c;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1&lt;BR /&gt;ch4 ch5 funded 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data d;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7&lt;BR /&gt;ch4 ch5 total 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%macro caldiff(ds1=, ds2=, fundvar=);&lt;BR /&gt;options mprint mlogic symbolgen;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct name into: charvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='CHAR' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct name into: numvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct strip(name)||"_N" into: newnumvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds2(drop=&amp;amp;numvars i);&lt;BR /&gt;set &amp;amp;ds2;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;do i=1 to dim(old);&lt;BR /&gt;new{i} = old{i};&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds1._new(drop=&amp;amp;newnumvars j);&lt;BR /&gt;merge &amp;amp;ds1(in=fund) &amp;amp;ds2(in=tot);&lt;BR /&gt;by &amp;amp;charvars;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;&lt;BR /&gt;if fund and tot then do;&lt;BR /&gt;do j=1 to dim(old);&lt;BR /&gt;old{j} = new{j} - old{j};&lt;BR /&gt;end;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend caldiff;&lt;BR /&gt;%caldiff(ds1=c, ds2=d, fundvar=c3);&lt;BR /&gt;%caldiff(ds1=a, ds2=b, fundvar=c3);&lt;BR /&gt;&lt;BR /&gt;For Example, I want to exclude N1 variable from calculation. Could someone please help me in issue.</description>
      <pubDate>Wed, 09 Jan 2019 16:56:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-function-to-exclude-date-and-selected-numeric-variables/m-p/525777#M143076</guid>
      <dc:creator>teja04</dc:creator>
      <dc:date>2019-01-09T16:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a function to exclude date and selected numeric variables using dictionary.colums</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-function-to-exclude-date-and-selected-numeric-variables/m-p/525792#M143082</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113781"&gt;@teja04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi all,&lt;BR /&gt;I have two dates with same variables. Before merging these two datasets for calculation. I want to exclude selected numeric variables and all data variables.&lt;BR /&gt;Below is my code:&lt;BR /&gt;data a;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1 2 3&lt;BR /&gt;ch4 ch5 funded 4 5 6&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data b;&lt;BR /&gt;input c1 $ c2 $ c3 $ n1 n2 n3;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7 8 9&lt;BR /&gt;ch4 ch5 total 10 11 12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data c;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 funded 1&lt;BR /&gt;ch4 ch5 funded 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data d;&lt;BR /&gt;input c1 $ c2 $ c3 $ val;&lt;BR /&gt;datalines;&lt;BR /&gt;ch1 ch2 total 7&lt;BR /&gt;ch4 ch5 total 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%macro caldiff(ds1=, ds2=, fundvar=);&lt;BR /&gt;options mprint mlogic symbolgen;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct name into: charvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='CHAR' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct name into: numvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;select distinct strip(name)||"_N" into: newnumvars separated by ' ' from dictionary.columns where memname="%upcase(&amp;amp;ds1)" and&lt;BR /&gt;libname='WORK' and upcase(type)='NUM' and upcase(name) ne "%upcase(&amp;amp;fundvar)";&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds2(drop=&amp;amp;numvars i);&lt;BR /&gt;set &amp;amp;ds2;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;do i=1 to dim(old);&lt;BR /&gt;new{i} = old{i};&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;ds1._new(drop=&amp;amp;newnumvars j);&lt;BR /&gt;merge &amp;amp;ds1(in=fund) &amp;amp;ds2(in=tot);&lt;BR /&gt;by &amp;amp;charvars;&lt;BR /&gt;array old {*} &amp;amp;numvars;&lt;BR /&gt;array new {*} &amp;amp;newnumvars;&lt;BR /&gt;&lt;BR /&gt;if fund and tot then do;&lt;BR /&gt;do j=1 to dim(old);&lt;BR /&gt;old{j} = new{j} - old{j};&lt;BR /&gt;end;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend caldiff;&lt;BR /&gt;%caldiff(ds1=c, ds2=d, fundvar=c3);&lt;BR /&gt;%caldiff(ds1=a, ds2=b, fundvar=c3);&lt;BR /&gt;&lt;BR /&gt;For Example, I want to exclude N1 variable from calculation. Could someone please help me in issue.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please describe the rule for how to know that N1 is a variable to exclude.&lt;/P&gt;
&lt;P&gt;Or are you looking for a way to provide a list of values instead to use? If that is the case then perhaps you are looking for something like this:&lt;/P&gt;
&lt;PRE&gt;%macro caldiff(ds1=, ds2=, fundvar=);
   options mprint mlogic symbolgen;
   %let ds1 =%upcase(&amp;amp;ds1.);
   %let ds2 =%upcase(&amp;amp;ds2.);
   %let temp =%upcase(&amp;amp;fundvar.);
   %let fundvar=;
   /* this do loop creates a quoted list*/
   %do i=1 %to %sysfunc(countw(&amp;amp;temp));
      %let fundvar = &amp;amp;fundvar. %sysfunc(quote(%scan(&amp;amp;temp.,&amp;amp;i.)));
   %end;

   proc sql;
      select distinct name into: charvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='CHAR' and 
            upcase(name) not in ( &amp;amp;fundvar.);
      select distinct name into: numvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='NUM' and 
            upcase(name) not in (&amp;amp;fundvar.);
      select distinct strip(name)||"_N" into: newnumvars separated by ' ' 
      from dictionary.columns 
      where memname="&amp;amp;ds1." and
            libname='WORK' and 
            upcase(type)='NUM' and 
            upcase(name)  not in ( &amp;amp;fundvar.);
   quit;

/*other code*/
%mend;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might suggest that your code might look a little cleaner by %upcase the macro variables before the first use so the remaining is a bit easier to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also use of this construct:&lt;/P&gt;
&lt;PRE&gt;data &amp;amp;ds2(drop=&amp;amp;numvars i);
set &amp;amp;ds2;&lt;/PRE&gt;
&lt;P&gt;is extremely dangerous, especially while testing code as when the input and output set are the same as the original data set is completely replaced and determining where a code logic problem occurred may be extremely difficult.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note the use of the forum {I} icon to open a code box for pasting code. The main message windows will reformat text removing indents and such with resulting code hard to read.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 17:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-function-to-exclude-date-and-selected-numeric-variables/m-p/525792#M143082</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-01-09T17:50:31Z</dc:date>
    </item>
  </channel>
</rss>

