<?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: Select statement inside the data step (case-when) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830519#M328174</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&lt;BR /&gt;Many thanks for this!</description>
    <pubDate>Fri, 26 Aug 2022 09:53:42 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2022-08-26T09:53:42Z</dc:date>
    <item>
      <title>Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830512#M328169</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to use case-when logic to condtionally create a new variable - in the example below, I want to assign all observations with value below 10 to the first group an all other to the second group. Can I do it &lt;STRONG&gt;without&lt;/STRONG&gt; if-then/else block? I've found the examples of using select statement inside the data step but it does not work as expected - only the first record is assingned properly. What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	input var;
	datalines;
	1
	2
	3
	30
	40
	50
	;
run;

data test;
	set test;
	select (var);
		when (var &amp;lt; 10) target_group = 1;
		otherwise  target_group = 2;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 09:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830512#M328169</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-08-26T09:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830515#M328171</link>
      <description>&lt;P&gt;The page &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm#p0lp6b046frq1in17umc48w3oboo" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm#p0lp6b046frq1in17umc48w3oboo&lt;/A&gt; indicates the correct use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should use the code:&lt;/P&gt;
&lt;PRE&gt;	select;
		when (var &amp;lt; 10) target_group = 1;
		otherwise  target_group = 2;
	end;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 09:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830515#M328171</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2022-08-26T09:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830517#M328172</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3492"&gt;@JosvanderVelden&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;Do you have any idea why the intial code produced this strange result (only first observation in the first group)? I could understand if it produced an error because of incorrecet syntax. But it did something strange instead.</description>
      <pubDate>Fri, 26 Aug 2022 09:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830517#M328172</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-08-26T09:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830518#M328173</link>
      <description>&lt;P&gt;See this article about &lt;A href="https://blogs.sas.com/content/iml/2016/06/20/select-when-sas-data-step.html" target="_self"&gt;the SELECT-WHEN logic in the DATA step&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The SELECT-WHEN statement supports two syntaxes. The first is when you are matching a discrete set of values that you know about in advance. For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
	set test;
   condition = (var &amp;lt; 10);
	select (condition);
		when (1) target_group = 1;
		otherwise  target_group = 2;
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the other syntax, you do not specify the name of the variable on the SELECT statement, and you&amp;nbsp; put the logic in the WHEN statement, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test3;
	set test;
	select;
		when (var &amp;lt; 10) target_group = 1;
		otherwise  target_group = 2;
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Although you can use the SELECT-WHEN statement to create a new variable, you can also &lt;A href="https://blogs.sas.com/content/iml/2019/06/10/5-reasons-to-use-proc-format-to-recode-variables-in-sas.html" target="_self"&gt;use a SAS FORMAT to create the values that you want.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 09:50:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830518#M328173</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-26T09:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830519#M328174</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&lt;BR /&gt;Many thanks for this!</description>
      <pubDate>Fri, 26 Aug 2022 09:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830519#M328174</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-08-26T09:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Select statement inside the data step (case-when)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830520#M328175</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24842"&gt;@chris2377&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such a simple conditional assignment can indeed be written without any additional statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;
target_group=2-(var &amp;lt; 10);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The Boolean expression &lt;FONT face="courier new,courier"&gt;var &amp;lt; 10&lt;/FONT&gt; is evaluated to 1 if the inequality is true (which includes the case that &lt;FONT face="courier new,courier"&gt;var&lt;/FONT&gt; contains a missing value!), else to 0. So, two minus the result is exactly the desired value for &lt;FONT face="courier new,courier"&gt;target_group&lt;/FONT&gt;. This also explains the result of your SELECT statement: the "Boolean" 1 (for TRUE) happened to match your &lt;FONT face="courier new,courier"&gt;var&lt;/FONT&gt; value 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that I used a different output dataset name (WANT) to avoid overwriting the input dataset in case of incorrect logic.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 09:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-statement-inside-the-data-step-case-when/m-p/830520#M328175</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-26T09:57:55Z</dc:date>
    </item>
  </channel>
</rss>

