<?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: Extracting data using distinct table records from a pre-populated table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-data-using-distinct-table-records-from-a-pre/m-p/774909#M246316</link>
    <description>&lt;P&gt;What is this line of code supposed to do?&lt;/P&gt;
&lt;PRE&gt;%if Areas.area = 'area1' %then %do;
&lt;/PRE&gt;
&lt;P&gt;If you think it is supposed to extract the value of a variable from a data set an use it then that is conceptually wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro code does not "see" variables in data sets. You can explicitly make a macro variable from the contents of a data set using the CALL SYMPUTX or CALL SYMPUT functions that the macro processor then can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second thing is to the macro processor everything is text so quite often including quotes around values to compare is incorrect. If the value of a macro variable is area1 then you do not use: %if &amp;amp;somevar = 'area1' because that means the quotes are part of the value instead&amp;nbsp; %if &amp;amp;somevar = area1 %then %do ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want want to do something based on multiple records in a data set then perhaps you should be looking at CALL EXECUTE to create code from the contents of the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW for small example data sets a DATA Step that will create the data set is preferred.&lt;/P&gt;
&lt;P&gt;Attached SAS data sets mean we have to go through gyrations to move the data around as clicking on the set makes a local different library for us with the data and then we have to make sure that all your references match that location or move the data set.&lt;/P&gt;
&lt;P&gt;There are also some potential issues between different operating systems/versions of SAS creating the data set and who may be able to use it such as issue from language encoding. The data step we can all run and have a working data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&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, 18 Oct 2021 15:16:28 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-10-18T15:16:28Z</dc:date>
    <item>
      <title>Extracting data using distinct table records from a pre-populated table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-data-using-distinct-table-records-from-a-pre/m-p/774849#M246303</link>
      <description>&lt;P&gt;I would like extract data from a data base using information in a pre-populated table. The populated table called &amp;lt;required&amp;gt; is attached. Usually, I used to have one row where Processed = 'F'. This time around, the field Processed has two records (both ='F') and I would like to extract two distinct data sets from the database for each value 'F' on the &amp;lt;Processed&amp;gt; table. The database is on &amp;lt;server1&amp;gt; and has two tables sample and result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used the following code to extract One data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%LET determ = ('cryptosp','giardia', 'MICROCYST','phage 1','phage1',&lt;BR /&gt;'CHCl3','CHBrCl2','CHBR3','CHBR2CL','Al', 'As','B','Ba', 'Cd', 'Cl', 'CN', &lt;BR /&gt;'CN-Total', 'CN_RECOVERABLE','Colour', 'Conductivity', 'Cr', 'Cu',&lt;BR /&gt;'EC', 'F','F/A CL2', 'F_M/CL', 'FC', 'Fe', 'Hardness','Total Hardness','Hg','IC_Cl', 'IC_F', 'IC_NO2',&lt;BR /&gt;'IC_NO3', 'IC_SO4', 'Low Hg', 'Mn', 'MONO CL','Na','NH4', 'Ni', 'NO2',&lt;BR /&gt;'NO2 AQUA','NO3', 'Odour','Pb', 'pH', 'PHENOL','Sb',&lt;BR /&gt;'Se', 'SO4','SPC','HPC', 'Taste','TC','TDS','TDS_CALC','TOC','Total Hardness','Turb','U', 'Zn',&lt;BR /&gt;'Ca','Mg','K','Odour','Taste',&lt;BR /&gt;'N-Ratio_Calc','TTHM-Ratio')&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;%LET area1 = ('D-BOIT','D-GA_LUKA','D-TOWNLANDS','D-BARN_RES','D-P1_PL',&lt;BR /&gt;'D-P2_PL','D-P3_PL','D-P4_PL','D-P5_PL','D-RUST_P6','D-RUST_S_OUT')&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;%LET area2 = ('D-BLOEM_N1','D-BLOEM_N6','D-CLARK_N1','D-CLARK_N4','D-CLARK_N6','D-HEIDEL','D-LESLIE',&lt;BR /&gt;'M-A6_SPECIAL','M-A8_SPECIAL','M-A12_SPECIAL','M-A14_SPECIAL','M-A15_SPECIAL',&lt;BR /&gt;'M-A19_SPECIAL','M-A20_SPECIAL','M-A21_SPECIAL',&lt;BR /&gt;'M-B1_SPECIAL','M-B2_SPECIAL','M-B4_SPECIAL','M-B6_SPECIAL',&lt;BR /&gt;'M-B7_SPECIAL','M-B8_SPECIAL','M-B10_SPECIAL','M-B12_SPECIAL','M-B15_SPECIAL');&lt;/P&gt;
&lt;P&gt;%LET area3 = ('B-DOM','B-R_COMMUNITY','B-R_POLICE_STA','B-R_SCHOOL');&lt;/P&gt;
&lt;P&gt;%LET area4 = ('D-DA15','D-SASOL','D-SASOL_PS','D-SASOL_RES_OUT',&lt;BR /&gt;'M-A6_SPECIAL','M-A8_SPECIAL','M-A12_SPECIAL','M-A14_SPECIAL','M-A15_SPECIAL','M-A19_SPECIAL',&lt;BR /&gt;'M-A20_SPECIAL','M-A21_SPECIAL', 'M-B1_SPECIAL','M-B2_SPECIAL','M-B4_SPECIAL','M-B6_SPECIAL',&lt;BR /&gt;'M-B7_SPECIAL','M-B8_SPECIAL','M-B10_SPECIAL','M-B12_SPECIAL','M-B15_SPECIAL');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A data set was extracted from the database where area = 'AREA1'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;file EXECUTE1 temp;&lt;BR /&gt;set required;&lt;/P&gt;
&lt;P&gt;put "proc sql; " / "create table " Report_name " as " / " select sample.sample_number,sample.sampling_point,sample.sampled_date,result.name,&lt;BR /&gt;result.numeric_entry,result.status, result.reportable";&lt;BR /&gt;put " from server1.sample, server1.result" / " where sample.sample_number=result.sample_number and" / " sample.sampled_date between ";&lt;BR /&gt;put "'" start_date +(-1) "'d and" ;&lt;BR /&gt;put "'" end_date "'d" ;&lt;BR /&gt;put "and sample.sampling_point in &amp;amp;area1 and " /&lt;BR /&gt;" result.name in &amp;amp;determ and " ;&lt;BR /&gt;put "result.status = 'A' and " / " result.reportable = 'T';" / " quit;";&lt;/P&gt;
&lt;P&gt;run;&lt;BR /&gt;%include EXECUTE1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*The different areas in the database has distinct values and these values may change from time to time hence I thought using a small "%Let" macro to represent these values would be handy.*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given this, the other data set was extracted using the program below using Area = 'AREA2':&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;file EXECUTE2 temp;&lt;BR /&gt;set required;&lt;/P&gt;
&lt;P&gt;put "proc sql; " / "create table " Report_name " as " / " select sample.sample_number,sample.sampling_point,sample.sampled_date,result.name,&lt;BR /&gt;result.numeric_entry,result.status, result.reportable";&lt;BR /&gt;put " from server1.sample, server1.result" / " where sample.sample_number=result.sample_number and" / " sample.sampled_date between ";&lt;BR /&gt;put "'" start_date +(-1) "'d and" ;&lt;BR /&gt;put "'" end_date "'d" ;&lt;BR /&gt;put "and sample.sampling_point in &amp;amp;area2 and " /&lt;BR /&gt;" result.name in &amp;amp;determ and " ;&lt;BR /&gt;put "result.status = 'A' and " / " result.reportable = 'T';" / " quit;";&lt;/P&gt;
&lt;P&gt;run;&lt;BR /&gt;%include EXECUTE2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In trying to extract the two tables at the same time, I thought of using a macro as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%MACRO DATA_DATA;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table Areas as select area &lt;BR /&gt;from required;quit;&lt;BR /&gt;%if Areas.area = 'area1' %then %do;&lt;BR /&gt;data _null_;&lt;/P&gt;
&lt;P&gt;file EXECUTE1 temp;&lt;BR /&gt;set required;&lt;/P&gt;
&lt;P&gt;put "proc sql; " / "create table " Report_name " as " / " select sample.sample_number,sample.sampling_point,sample.sampled_date,result.name,&lt;BR /&gt;result.numeric_entry,result.status, result.reportable";&lt;BR /&gt;put " from server1.sample, server1.result" / " where sample.sample_number=result.sample_number and" / " sample.sampled_date between ";&lt;BR /&gt;put "'" start_date +(-1) "'d and" ;&lt;BR /&gt;put "'" end_date "'d" ;&lt;BR /&gt;put "and sample.sampling_point in &amp;amp;area1 and " /&lt;BR /&gt;" result.name in &amp;amp;determ and " ;&lt;BR /&gt;put "result.status = 'A' and " / " result.reportable = 'T';" / " quit;";&lt;/P&gt;
&lt;P&gt;run;&lt;BR /&gt;%END;%else&lt;/P&gt;
&lt;P&gt;%if Areas.area = 'area2' %then %do;&lt;BR /&gt;data _null_;&lt;/P&gt;
&lt;P&gt;file EXECUTE2 temp;&lt;BR /&gt;set required;&lt;/P&gt;
&lt;P&gt;put "proc sql; " / "create table " Report_name " as " / " select sample.sample_number,sample.sampling_point,sample.sampled_date,result.name,&lt;BR /&gt;result.numeric_entry,result.status, result.reportable";&lt;BR /&gt;put " from server1.sample, server1.result" / " where sample.sample_number=result.sample_number and" / " sample.sampled_date between ";&lt;BR /&gt;put "'" start_date +(-1) "'d and" ;&lt;BR /&gt;put "'" end_date "'d" ;&lt;BR /&gt;put "and sample.sampling_point in &amp;amp;area2 and " /&lt;BR /&gt;" result.name in &amp;amp;determ and " ;&lt;BR /&gt;put "result.status = 'A' and " / " result.reportable = 'T';" / " quit;";&lt;/P&gt;
&lt;P&gt;run;&lt;BR /&gt;%END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%MEND DATA_DATA;&lt;BR /&gt;%DATA_DATA;%include EXECUTE1;%include EXECUTE2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, the macro gives me REP001 data and REP015 data which are the same.&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, 18 Oct 2021 09:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-data-using-distinct-table-records-from-a-pre/m-p/774849#M246303</guid>
      <dc:creator>mmohotsi</dc:creator>
      <dc:date>2021-10-18T09:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting data using distinct table records from a pre-populated table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-data-using-distinct-table-records-from-a-pre/m-p/774909#M246316</link>
      <description>&lt;P&gt;What is this line of code supposed to do?&lt;/P&gt;
&lt;PRE&gt;%if Areas.area = 'area1' %then %do;
&lt;/PRE&gt;
&lt;P&gt;If you think it is supposed to extract the value of a variable from a data set an use it then that is conceptually wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro code does not "see" variables in data sets. You can explicitly make a macro variable from the contents of a data set using the CALL SYMPUTX or CALL SYMPUT functions that the macro processor then can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second thing is to the macro processor everything is text so quite often including quotes around values to compare is incorrect. If the value of a macro variable is area1 then you do not use: %if &amp;amp;somevar = 'area1' because that means the quotes are part of the value instead&amp;nbsp; %if &amp;amp;somevar = area1 %then %do ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want want to do something based on multiple records in a data set then perhaps you should be looking at CALL EXECUTE to create code from the contents of the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW for small example data sets a DATA Step that will create the data set is preferred.&lt;/P&gt;
&lt;P&gt;Attached SAS data sets mean we have to go through gyrations to move the data around as clicking on the set makes a local different library for us with the data and then we have to make sure that all your references match that location or move the data set.&lt;/P&gt;
&lt;P&gt;There are also some potential issues between different operating systems/versions of SAS creating the data set and who may be able to use it such as issue from language encoding. The data step we can all run and have a working data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&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, 18 Oct 2021 15:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-data-using-distinct-table-records-from-a-pre/m-p/774909#M246316</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-18T15:16:28Z</dc:date>
    </item>
  </channel>
</rss>

