<?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 How to get rows into a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913579#M360069</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp; a table which contains&amp;nbsp; values of "if then else conditions" in a variable. how can loop them in a data step.&lt;/P&gt;
&lt;P&gt;currently we are copying all rows from that column into a data step like below&lt;/P&gt;
&lt;PRE&gt;data have;
SET test
(WHERE=(TIMESTAMP  BETWEEN &amp;amp;START. AND &amp;amp;END.));

/* row1 */  IF ( domain GE 21.00000 ) THEN name = '03. &amp;gt;= 21.00000';         
/* row2 */  IF ( SYSTEM EQ -999999 ) THEN op = '01. Especial Value = -999999'; ELSE 
/* row3 */  IF ( SYSTEM LT 17.00000 ) THEN op = '02. &amp;lt; 17.00000'; ELSE 
/* row4 */  IF ( SYSTEM GE 17.00000 AND SYSTEM LT 20.00000 ) THEN op = '03. &amp;gt;= 17.00000 and &amp;lt; 20.00000'; ELSE 
/* row5 */  IF ( SYSTEM GE 20.00000 ) THEN op = '04. &amp;gt;= 20.00000';
/* row6 */  IF ( region LT 2.00000 ) THEN LN = '01. &amp;lt; 2.00000'; ELSE 
/* row7 */  IF ( region GE 2.00000 AND region LT 10.00000 ) THEN LN = '02. &amp;gt;= 2.00000 and &amp;lt; 10.00000'; ELSE 
/* row8 */  IF ( region GE 10.00000 AND region LT 11.00000 ) THEN LN = '03. &amp;gt;= 10.00000 and &amp;lt; 11.00000'; ELSE 
/* row9 */  IF ( region GE 11.00000 ) THEN LN = '04. &amp;gt;= 11.00000';         
/* row10 */  IF ( status LT 2.00000 ) THEN cn = '01. &amp;lt; 2.00000'; ELSE 
/* row11 */  IF ( status GE 2.00000 AND status LT 4.00000 ) THEN cn = '02. &amp;gt;= 2.00000 and &amp;lt; 4.00000'; ELSE 
/* row12 */  IF ( status GE 4.00000 AND status LT 7.00000 ) THEN cn = '03. &amp;gt;= 4.00000 and &amp;lt; 7.00000'; ELSE 
/* row13 */  IF ( status GE 7.00000 ) THEN cn = '04. &amp;gt;= 7.00000';


run;&lt;/PRE&gt;
&lt;P&gt;I am looking like below macro that loops all rows.&lt;/P&gt;
&lt;PRE&gt;data have;
SET test
(WHERE=(TIMESTAMP  BETWEEN &amp;amp;START. AND &amp;amp;END.));

&amp;lt;&amp;amp;rows&amp;gt;;


run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Sathya.&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jan 2024 13:52:29 GMT</pubDate>
    <dc:creator>sathya66</dc:creator>
    <dc:date>2024-01-30T13:52:29Z</dc:date>
    <item>
      <title>How to get rows into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913579#M360069</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp; a table which contains&amp;nbsp; values of "if then else conditions" in a variable. how can loop them in a data step.&lt;/P&gt;
&lt;P&gt;currently we are copying all rows from that column into a data step like below&lt;/P&gt;
&lt;PRE&gt;data have;
SET test
(WHERE=(TIMESTAMP  BETWEEN &amp;amp;START. AND &amp;amp;END.));

/* row1 */  IF ( domain GE 21.00000 ) THEN name = '03. &amp;gt;= 21.00000';         
/* row2 */  IF ( SYSTEM EQ -999999 ) THEN op = '01. Especial Value = -999999'; ELSE 
/* row3 */  IF ( SYSTEM LT 17.00000 ) THEN op = '02. &amp;lt; 17.00000'; ELSE 
/* row4 */  IF ( SYSTEM GE 17.00000 AND SYSTEM LT 20.00000 ) THEN op = '03. &amp;gt;= 17.00000 and &amp;lt; 20.00000'; ELSE 
/* row5 */  IF ( SYSTEM GE 20.00000 ) THEN op = '04. &amp;gt;= 20.00000';
/* row6 */  IF ( region LT 2.00000 ) THEN LN = '01. &amp;lt; 2.00000'; ELSE 
/* row7 */  IF ( region GE 2.00000 AND region LT 10.00000 ) THEN LN = '02. &amp;gt;= 2.00000 and &amp;lt; 10.00000'; ELSE 
/* row8 */  IF ( region GE 10.00000 AND region LT 11.00000 ) THEN LN = '03. &amp;gt;= 10.00000 and &amp;lt; 11.00000'; ELSE 
/* row9 */  IF ( region GE 11.00000 ) THEN LN = '04. &amp;gt;= 11.00000';         
/* row10 */  IF ( status LT 2.00000 ) THEN cn = '01. &amp;lt; 2.00000'; ELSE 
/* row11 */  IF ( status GE 2.00000 AND status LT 4.00000 ) THEN cn = '02. &amp;gt;= 2.00000 and &amp;lt; 4.00000'; ELSE 
/* row12 */  IF ( status GE 4.00000 AND status LT 7.00000 ) THEN cn = '03. &amp;gt;= 4.00000 and &amp;lt; 7.00000'; ELSE 
/* row13 */  IF ( status GE 7.00000 ) THEN cn = '04. &amp;gt;= 7.00000';


run;&lt;/PRE&gt;
&lt;P&gt;I am looking like below macro that loops all rows.&lt;/P&gt;
&lt;PRE&gt;data have;
SET test
(WHERE=(TIMESTAMP  BETWEEN &amp;amp;START. AND &amp;amp;END.));

&amp;lt;&amp;amp;rows&amp;gt;;


run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Sathya.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 13:52:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913579#M360069</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2024-01-30T13:52:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to get rows into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913580#M360070</link>
      <description>&lt;P&gt;I believe I understand what you are trying to do (although I am not 100% sure I do understand properly).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have a data set with text strings that are actually SAS IF-THEN-ELSE commands. I'm going to call this data set DATASET_WITH_IF_THEN_ELSE;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Furthermore, in this data set, you have a text string which is in the variable named COMMAND.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this really matches your situation (although possibly with different data set name and variable name), then this will work for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */
data _null_;
    set dataset_with_if_then_else end=eof;
    call execute('data have;
        set test(where=(timestamp between &amp;amp;start end &amp;amp;end));
        ');
    call execute(command);
    if eof then call execute('run;');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the commands are not in a SAS data set but just a plain text file, which I have creatively named text_file.txt, then this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */
data have;
    set test(where=(timestamp between &amp;amp;start end &amp;amp;end));
    %include "text_file.txt";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;If the above description of the problem is off target somehow, please explain further.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 14:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913580#M360070</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-30T14:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to get rows into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913581#M360071</link>
      <description>&lt;P&gt;Thanks, You understood it correctly. I have SAS IF-THEN-ELSE commands in a variable(ie : command variable)&amp;nbsp; in test table only not in a different table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so my code will be like below?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* UNTESTED CODE */
data _null_;
    set test end=eof;
    call execute('data have;
        set test(where=(timestamp between &amp;amp;start end &amp;amp;end));
        ');
    call execute(command);
    if eof then call execute('run;');
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2024 14:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913581#M360071</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2024-01-30T14:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to get rows into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913585#M360075</link>
      <description>&lt;P&gt;Forget CALL EXECUTE().&amp;nbsp; Just treat the data as data.&amp;nbsp; Run a data step to write the code into an actual code file.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set test;
  file code;
  put command;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use %INCLUDE to run the code wherever you need it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
   set test (drop=command);
   where (timestamp between &amp;amp;start and &amp;amp;end) ;
%include code / source2 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is not at all clear to me if the WHERE belongs on the first step or the second (or both).&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 14:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913585#M360075</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-30T14:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to get rows into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913604#M360085</link>
      <description>&lt;P&gt;Quite often these sorts of If/then/else when using a single value to create or assign a new value to a different variable can be done with custom formats. Which moves such logic out of the data step.&lt;/P&gt;
&lt;P&gt;If the repeated use of your new values starting with 01. 02. 03. etc is there for sort order purposes then a format, since it applies to the underlying value, would display in proper numeric value order (unless used in procedures that explicitly allow changing order to use formatted values).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then running this code is not even needed.&lt;/P&gt;
&lt;P&gt;Example of one format:&lt;/P&gt;
&lt;PRE&gt;Proc format;
value op
-999999 =   '01. Especial Value = -999999'
/* the -999999 below would likely better be the smallest 
   value you expect for the variable
*/
-999999 &amp;lt; - 17.00000 =  '02. &amp;lt; 17.00000'
17.00000 - &amp;lt;20.00000 =  '03. &amp;gt;= 17.00000 and &amp;lt; 20.00000'
20.00000 - high = '04. &amp;gt;= 20.00000'
;
run;&lt;/PRE&gt;
&lt;P&gt;One example of use:&lt;/P&gt;
&lt;PRE&gt;Proc print data=yourdatasetwithSystem ;
   var system;
   format system op. ;
run;&lt;/PRE&gt;
&lt;P&gt;Formats have a great deal of flexibility that is not easy with if/then/else code. First the same format can be applied to multiple variables with a single statement:&lt;/P&gt;
&lt;P&gt;format thisvar thatvar anothervar varlist1-varlist25 customformatname. ;&lt;/P&gt;
&lt;P&gt;another is that formats can be stored between sessions in permanent libraries to be available when needed.&lt;/P&gt;
&lt;P&gt;The formats can also be placed into data sets for sharing with the Proc Format Cntlin and Cntlout options.&lt;/P&gt;
&lt;P&gt;Another feature might address what appears to be 2 missing "rows" in your example. You start with&lt;/P&gt;
&lt;PRE&gt;/* row1 */  IF ( domain GE 21.00000 ) THEN name = '03. &amp;gt;= 21.00000';    &lt;/PRE&gt;
&lt;P&gt;But do not show, following the pattern for your other variables, what should be done with any DOMAIN with values less than 21, or "special" values.&lt;/P&gt;
&lt;P&gt;The Proc format Value option "OTHER=" creates the value for any value not explicitly listed. Combined with the _error_ option it will also throw a log entry for any unexpected values encountered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, with this:&lt;/P&gt;
&lt;PRE&gt;/* row6 */  IF ( region LT 2.00000 ) THEN LN = '01. &amp;lt; 2.00000'; ELSE &lt;/PRE&gt;
&lt;P&gt;when Region has a missing value do you actually want a result of '01. &amp;lt;2.0000' ? That is what the code will do.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 15:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-rows-into-a-data-step/m-p/913604#M360085</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-30T15:37:47Z</dc:date>
    </item>
  </channel>
</rss>

