<?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: Check columns whether they are present in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520062#M140974</link>
    <description>It may help if you explain what 'didn't work' and show a better example of what you have and what you need.</description>
    <pubDate>Mon, 10 Dec 2018 17:26:02 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-12-10T17:26:02Z</dc:date>
    <item>
      <title>Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519827#M140878</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;I have&amp;nbsp; a dataset&amp;nbsp; Test1 which has these columns Placebo,RBC,WBC,Weight,Number.Another Dataset Test2 which looks like below. In Dataset Test2 I have taken list of items I need to check in Data Test1 whether they exist or not .&amp;nbsp; And for the match ones i need to create Test3 which will have common variables which are in both Test1 and Test2 dataset using below code i am able to check all the variables&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Test2&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;List&lt;/TD&gt;&lt;TD&gt;Freq&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RBC&lt;/TD&gt;&lt;TD&gt;Count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WBC&lt;/TD&gt;&lt;TD&gt;Treatment&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Placebo&lt;/TD&gt;&lt;TD&gt;Other,Race,Weight&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc contents data=test1&amp;nbsp; out=sdetail (keep=name varnum);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;&amp;nbsp;create table Test3 as&lt;BR /&gt;&amp;nbsp;&amp;nbsp;select List,Freq&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;,(upcase(List) in (select upcase(name) from sdetail)) as var1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;,(upcase(Freq) in (select upcase(name) from sdetail)) as var2&lt;BR /&gt;&amp;nbsp;&amp;nbsp;from test2&lt;/P&gt;&lt;P&gt;create &amp;nbsp;table&amp;nbsp; Test7 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select List , Freq from test3&lt;/P&gt;&lt;P&gt;&amp;nbsp; where var1=1 and var2=1&lt;BR /&gt;&amp;nbsp;;&lt;BR /&gt;&amp;nbsp;quit;&lt;/P&gt;&lt;P&gt;But I am able to check Count,Treatment RBC,WBC in test2 whether they are in test1 but how can i check when variables in Freq seperated by commas using above code like other,Race,Weight&lt;/P&gt;&lt;P&gt;Can anyone please help&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 05:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519827#M140878</guid>
      <dc:creator>hexx18</dc:creator>
      <dc:date>2018-12-10T05:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519828#M140879</link>
      <description>&lt;P&gt;If you're checking if two data sets have the same variables, have you considered PROC COMPARE?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I also have a little routine here that creates a data set that identifies which variables are in which data sets.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro compare_data(base=, compare=);
proc sql noprint;
  create table base_in as
  select name
  from sashelp.vcolumn
  where libname=upper(scan("&amp;amp;base", 1, "."))
  and memname=upper(scan("&amp;amp;base", 2, "."))
  order by varnum;
quit;
proc sql noprint;
  create table compare_in as
  select name
  from sashelp.vcolumn
  where libname=upper(scan("&amp;amp;compare", 1, "."))
  and memname=upper(scan("&amp;amp;compare", 2, "."))
  order by varnum;
quit;
proc sql;
  create table comparison as
  select a.name as base_var, b.name as compare_var,
  case when missing(a.name) then catx("-", "Comparison dataset has extra variable", b.name)
  when missing(b.name) then catx("-", "Comparison dataset is missing variable", a.name)
  when a.name=b.name then "Variable in both datasets"
  else "CHECKME"
  end as comparison
  from base_in as a
  full join compare_in as b
  on a.name=b.name;
quit;
data _null_;
set comparison;
if comparison not eq "Variable in both datasets"
  then put "ERROR:" comparison;
run;
%mend;


/*example of calling macro*/

data class;
set sashelp.class;
drop sex age;
weight2=weight*2;
run;

%compare_data(base=sashelp.class, compare=work.class);
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/e0b98c4627aa31a567e5" target="_blank"&gt;https://gist.github.com/statgeek/e0b98c4627aa31a567e5&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 03:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519828#M140879</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-10T03:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519833#M140882</link>
      <description>&lt;P&gt;Thank you . But I wanted test3 table which is a result of var1=1 and var2=1 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;If both RBC and Count from test2 present in test1 then var1=1 and var2=1&lt;/TD&gt;&lt;TD&gt;we need this row in test3table&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;If WBC from test2 is present in test1 and Treatment is not present in test1 then var1=1 and var2=0&lt;/TD&gt;&lt;TD&gt;we don’t need this row in test3 table&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;but how do I check when variables seperated by commas&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;?&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 05:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519833#M140882</guid>
      <dc:creator>hexx18</dc:creator>
      <dc:date>2018-12-10T05:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519846#M140891</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="无标题1.jpg" style="width: 572px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/25485i592A09641099B470/image-size/large?v=v2&amp;amp;px=999" role="button" title="无标题1.jpg" alt="无标题1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 07:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519846#M140891</guid>
      <dc:creator>learsaas</dc:creator>
      <dc:date>2018-12-10T07:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519863#M140898</link>
      <description>&lt;P&gt;Please stop posting code as pictures.&amp;nbsp; There is above the post area a code window - its the {i} - where you can copy text from your editor and post it into the code window here.&amp;nbsp; This keeps formatting (mostly), and allows other users to copy and paste the code as well.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 08:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/519863#M140898</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-10T08:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520057#M140970</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried it didn't work . I wanted something like when Freq has variables which are separated by commas we should use some index to check if we have comma and then if yes loop through all variables to check whether each variable is present in sdetail dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 17:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520057#M140970</guid>
      <dc:creator>hexx18</dc:creator>
      <dc:date>2018-12-10T17:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520062#M140974</link>
      <description>It may help if you explain what 'didn't work' and show a better example of what you have and what you need.</description>
      <pubDate>Mon, 10 Dec 2018 17:26:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520062#M140974</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-10T17:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Check columns whether they are present</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520288#M141050</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*1*/
proc contents data=test1  out=sdetail (keep=name) noprint;
run;
data sdetail;
	set sdetail;
	name=upcase(name);
run;
data test7;
	if 0 then set test2 sdetail(keep=name);
	if _n_=1 then do;
		declare hash myhash(dataset:'sdetail');
		myhash.definekey('name');
		myhash.definedone();
	end;
	set test2;
	length temp1 $32;
	length temp2 $32;
	n1=countw(List, ',');n2=countw(Freq, ',');
	Flag1=1;Flag2=1;
	do i=1 to n1;
		temp1=upcase(scan(List,i,','));
		if myhash.check(key:temp1)^=0 and temp1^='' then do;
			Flag1=0;leave;
		end;
	end;
	if Flag1=1 then do i=1 to n2;
		temp2=upcase(scan(Freq,i,','));
		if myhash.check(key:temp2)^=0 and temp2^='' then do;
			Flag2=0;leave;
		end;
	end;
	if Flag1 and Flag2;
	keep List Freq;
run;

/* 2*/
proc contents data=test1  out=sdetail (keep=name) noprint;
run;
%let strFieldList=;
proc sql noprint;
	select name into :strFieldList separated by ',' from sdetail;
quit;
%put &amp;amp;strFieldList;
data test7;
	set test2;
	length temp1 $32;
	length temp2 $32;
	n1=countw(List, ',');n2=countw(Freq, ',');
	Flag1=1;Flag2=1;
	do i=1 to n1;
		temp1=scan(List,i,',');
		if temp1^='' and find("&amp;amp;strFieldList",temp1,'it')=0 then do;
			Flag1=0;leave;
		end;
	end;
	if Flag1=1 then do i=1 to n2;
		temp2=scan(Freq,i,',');
		if temp2^='' and find("&amp;amp;strFieldList",temp2,'it')=0 then do;
			Flag2=0;leave;
		end;
	end;
	if Flag1 and Flag2;
	keep List Freq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Dec 2018 09:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-columns-whether-they-are-present/m-p/520288#M141050</guid>
      <dc:creator>learsaas</dc:creator>
      <dc:date>2018-12-11T09:04:54Z</dc:date>
    </item>
  </channel>
</rss>

