<?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: Conditions in a column as a macro variable to identify every row in a dataset. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638034#M189727</link>
    <description>&lt;P&gt;The SEP should be removed.&amp;nbsp; It was left over from a different coding pattern for how to handle the conditional generation of the ELSE keyword.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Apr 2020 12:40:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-04-07T12:40:22Z</dc:date>
    <item>
      <title>Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637946#M189685</link>
      <description>&lt;P&gt;Hey.&lt;/P&gt;&lt;P&gt;Let's consider the table 'Cars' and table 'Dictionary'.&lt;/P&gt;&lt;P&gt;Table dictionary has two variables, first: ID of a row, second: condition which determines what ID has a particular row.&lt;/P&gt;&lt;P&gt;For instance, where a record:&lt;/P&gt;&lt;P&gt;Type='SUV' and Horsepower &amp;gt; 300 and Cylinders &amp;gt; 6 THEN ID=TYPE_A&lt;/P&gt;&lt;P&gt;Now, using var_condition and var_id I would like to flag every record of a dataset by an 'ID' and stack them in a new table. I tried simple macro %if &amp;amp;var_condition then new_column = &amp;amp;ID, but I am not even close. How can I approach this task?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
set sashelp.cars;
run;

data Dictionary;
   input ID : $6. Condition &amp;amp; $211.;
datalines;
TYPE_A upcase(Type) IN('SUV', 'SPORTS', 'WAGON') and upcase(Origin) = 'ASIA' AND Horsepower &amp;gt; 150
TYPE_B Horsepower &amp;gt;= 220 and upcase(Origin)='EUROPE'
TYPE_C Type='SUV' and Horsepower &amp;gt; 300 and Cylinders &amp;gt; 6
;
proc sql noprint;
select ID into :var_id seprated by ' '
from Dictionary;
proc sql noprint;
select Condition into :var_condition seprated by ' '
from Dictionary;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Apr 2020 22:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637946#M189685</guid>
      <dc:creator>nowak22</dc:creator>
      <dc:date>2020-04-06T22:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637948#M189686</link>
      <description>&lt;P&gt;Did you read your log?&lt;/P&gt;
&lt;P&gt;Dictionary is a special word for proc sql and when you do not specify the library that such as with WORK.Dictionary then proc sql expects a name of one of the dictionary tables that SAS maintains such as Dictionary.Tables, Dictionary.columns or others.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So a a minimum you need code similar to:&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
select ID into :var_id separated by ' '
from work.Dictionary;
quit;
proc sql noprint;
select Condition into :var_condition separated by ' '
from Work.Dictionary;
quit;&lt;/PRE&gt;
&lt;P&gt;Note correction in "separated" as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't show anything about how you actually attempted to use the macro variables.&lt;/P&gt;
&lt;P&gt;Since %IF would be applied in the compile phase of a data step it is very likely that you compared something not available to the data step.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 20:27:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637948#M189686</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-07T20:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637954#M189687</link>
      <description>&lt;P&gt;I'm guessing you are wanting to use the types (ID) to pass into some sort of where clause where the condition is then applied. If I'm guessing right, then you'd take your dataset with your ID &amp;amp; condition text, then use data _null_ with call symputx to create macro variables for each type, that would hold the conditional text string. Then you use the macro variable to refer to the condition in later where statements or SQL clauses. Is this what you're trying to do?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
set sashelp.cars;
run;

data Dictionary;
   input ID : $6. Condition &amp;amp; $211.;
datalines;
TYPE_A upcase(Type) IN('SUV', 'SPORTS', 'WAGON') and upcase(Origin) = 'ASIA' AND Horsepower &amp;gt; 150
TYPE_B Horsepower &amp;gt;= 220 and upcase(Origin)='EUROPE'
TYPE_C Type='SUV' and Horsepower &amp;gt; 300 and Cylinders &amp;gt; 6
;
data _null_;&lt;BR /&gt;set dictionary;
call symputx(id,condition);
run;

data want;
set sashelp.cars;
where &amp;amp;TYPE_A.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Apr 2020 22:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637954#M189687</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2020-04-06T22:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637956#M189689</link>
      <description>&lt;P&gt;You definitely don't want to generate a macro %IF statement if you want to apply the condition to actual data.&lt;/P&gt;
&lt;P&gt;Step 1 is to write the SAS code you want to generate.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set cars;
  length id $6 ;
  if  upcase(Type) IN('SUV', 'SPORTS', 'WAGON') and upcase(Origin) = 'ASIA'
      AND Horsepower &amp;gt; 150 then id="TYPE_A";
  else if Horsepower &amp;gt;= 220 and upcase(Origin)='EUROPE' then id="TYPE_B ";
  else if Type='SUV' and Horsepower &amp;gt; 300 and Cylinders &amp;gt; 6 then id="TYPE_C";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you get that working then you can try to figure out how to generate it from the data you have.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set dictionary;
  file code;
  if _n_ &amp;gt; 1 then put ' else' @ ;
  put ' if ' condition 'then id=' id :$quote. ';' ;
run;
data want;
  set cards;
  length id $6;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Updated:&lt;/STRONG&gt; to remove spurious SEP variable reference in PUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 12:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637956#M189689</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-07T12:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637995#M189704</link>
      <description>&lt;P&gt;Maxim 18: Separate Your Names.&lt;/P&gt;
&lt;P&gt;"Dictionary" is a "keyword" in SQL and should therefore not be used for names of objects (datasets, variables).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which of your conditions should take precedence if an observation meets more than one condition (e.g. an SUV from Asia or Europe with 8 cylinders and 350 horsepower)?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 07:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637995#M189704</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-07T07:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637997#M189706</link>
      <description>Thank you for your replay.&lt;BR /&gt;That is something that I am hoping to achieve.&lt;BR /&gt;However, if the record fulfills the condition, then I would like to mark it using the ID column. If it does not, then I would like to check for second condition and mark it by ID, until it goes through every condition.</description>
      <pubDate>Tue, 07 Apr 2020 07:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/637997#M189706</guid>
      <dc:creator>nowak22</dc:creator>
      <dc:date>2020-04-07T07:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638001#M189710</link>
      <description>Thank you for your replay, you are certainly right about naming datasets.&lt;BR /&gt;&lt;BR /&gt;About the precedence:&lt;BR /&gt;Check the first condition (first row from column Condition) if true, then mark it as first row from column ID, if not, proceed to the second row, until end of the column Condition, until the end of the conditions.&lt;BR /&gt;</description>
      <pubDate>Tue, 07 Apr 2020 07:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638001#M189710</guid>
      <dc:creator>nowak22</dc:creator>
      <dc:date>2020-04-07T07:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638002#M189711</link>
      <description>&lt;P&gt;Then&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s code provides the solution.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 08:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638002#M189711</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-07T08:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638005#M189713</link>
      <description>&lt;P&gt;Hey, thank your for your replay.&lt;/P&gt;&lt;P&gt;Does this code compiles without an error for you?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
set sashelp.cars;
run;

data dictionary;
   input ID : $6. Condition &amp;amp; $211.;
datalines;
TYPE_A upcase(Type) IN('SUV', 'SPORTS', 'WAGON') and upcase(Origin) = 'ASIA' AND Horsepower &amp;gt; 150
TYPE_B Horsepower &amp;gt;= 220 and upcase(Origin)='EUROPE'
TYPE_C Type='SUV' and Horsepower &amp;gt; 300 and Cylinders &amp;gt; 6
;

filename code temp;
data _null_;
  set dictionary;
  file code;
  if _n_ &amp;gt; 1 then put 'else' @ ;
  put sep ' if ' condition 'then id=' id :$quote. ';' ;
run;

data want;
  set cars;
  length id $6;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am ha&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 08:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638005#M189713</guid>
      <dc:creator>nowak22</dc:creator>
      <dc:date>2020-04-07T08:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638007#M189715</link>
      <description>&lt;P&gt;What should "sep" do in this statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put sep ' if ' condition 'then id=' id :$quote. ';' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your code seems to work well without it.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 09:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638007#M189715</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-07T09:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638008#M189716</link>
      <description>Yep, thank you very much both of you.&lt;BR /&gt;With "sep" I am having an error, without it, it works fine.</description>
      <pubDate>Tue, 07 Apr 2020 09:44:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638008#M189716</guid>
      <dc:creator>nowak22</dc:creator>
      <dc:date>2020-04-07T09:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditions in a column as a macro variable to identify every row in a dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638034#M189727</link>
      <description>&lt;P&gt;The SEP should be removed.&amp;nbsp; It was left over from a different coding pattern for how to handle the conditional generation of the ELSE keyword.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 12:40:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditions-in-a-column-as-a-macro-variable-to-identify-every-row/m-p/638034#M189727</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-07T12:40:22Z</dc:date>
    </item>
  </channel>
</rss>

