<?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 Wrongfully assigning a value to a variable using &amp;quot;if&amp;quot; in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692631#M24927</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;This post is a follow-up of the previous one with more detail -honestly, I'm not entirely sure what happened to the previous question. The problematic piece of code is this one:&lt;/P&gt;&lt;P&gt;_________&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;input visit $16.;&lt;BR /&gt;datalines;&lt;BR /&gt;First&lt;BR /&gt;Second&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data nulll;&lt;BR /&gt;input index;&lt;BR /&gt;datalines;&lt;BR /&gt;.&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;stop;&lt;BR /&gt;set nulll;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set test2 test1;&lt;BR /&gt;if index(VISIT, 'Fir')^=0 then do;&lt;BR /&gt;index=100;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data=test;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;__________&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;The output variable "index" contains two values of 100 whereas the logic implies that only one should be there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On the other hand if we split the last data step in two it works properly (that is, firstly set test1 and test2 to get test and then set test to do the procedures) but I'm afraid to post the full code as it may get flagged as spam.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please explain what the issue is - I believe it's something to do with setting the datasets and performing operations on the data in the same step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;teli4&lt;/P&gt;</description>
    <pubDate>Mon, 19 Oct 2020 17:26:10 GMT</pubDate>
    <dc:creator>teli4</dc:creator>
    <dc:date>2020-10-19T17:26:10Z</dc:date>
    <item>
      <title>Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692631#M24927</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;This post is a follow-up of the previous one with more detail -honestly, I'm not entirely sure what happened to the previous question. The problematic piece of code is this one:&lt;/P&gt;&lt;P&gt;_________&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;input visit $16.;&lt;BR /&gt;datalines;&lt;BR /&gt;First&lt;BR /&gt;Second&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data nulll;&lt;BR /&gt;input index;&lt;BR /&gt;datalines;&lt;BR /&gt;.&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;stop;&lt;BR /&gt;set nulll;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set test2 test1;&lt;BR /&gt;if index(VISIT, 'Fir')^=0 then do;&lt;BR /&gt;index=100;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data=test;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;__________&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;The output variable "index" contains two values of 100 whereas the logic implies that only one should be there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On the other hand if we split the last data step in two it works properly (that is, firstly set test1 and test2 to get test and then set test to do the procedures) but I'm afraid to post the full code as it may get flagged as spam.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please explain what the issue is - I believe it's something to do with setting the datasets and performing operations on the data in the same step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;teli4&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 17:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692631#M24927</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-19T17:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692640#M24928</link>
      <description>&lt;P&gt;Can you describe why all the messing around with a 0 observation data set is necessary.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 17:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692640#M24928</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-19T17:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692644#M24929</link>
      <description>&lt;P&gt;Variables from incoming datasets are automatically retained. index is in dataset test2, so it is retained. With the first observation from test1, index is set to 100, and keeps that value, since it is never overwritten.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 17:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692644#M24929</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-19T17:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692645#M24930</link>
      <description>Hello there! The names of the given variables and the ones that should be obtained have to be taken from an external file and are fixed - the way this dummy dataset works is it ensures that the variables are properly named. Of course, the code above is simplified as much as I could and is just there to give a brief overview of the problem. Also, thanks for taking the time to consider this question!</description>
      <pubDate>Mon, 19 Oct 2020 17:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692645#M24930</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-19T17:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692647#M24931</link>
      <description>&lt;P&gt;Thank You very much for taking the time to answer! However, as I am at a beginner level, I'm a bit confused - with the set procedure I ensure that all variable from both data sets will be included (I'm assuming that's what you meant with retaining the variable index). However, shouldn't the logical operator "if" ensure that the condition is checked for each row, that is, for each observation of the variable 'visit'? That would imply that one of those logical checks would fail and one will not, so the value will be overwritten, no?&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 18:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692647#M24931</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-19T18:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692661#M24932</link>
      <description>&lt;P&gt;Your IF does not have an ELSE branch, so only the the action following the THEN branch will be executed, once. Since the resulting variable is retained, the value does not change thereafter.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 18:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692661#M24932</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-19T18:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692665#M24933</link>
      <description>&lt;P&gt;Thanks! So that means that the "if" operator does not check each row (i.e. each observation of the particular variable) if an "else" statement is not provided but rather uses only the outcome of the first true logical result and plugs it all the way down?&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 18:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692665#M24933</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-19T18:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692676#M24934</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/353112"&gt;@teli4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello there! The names of the given variables and the ones that should be obtained have to be taken from an external file and are fixed - the way this dummy dataset works is it ensures that the variables are properly named. Of course, the code above is simplified as much as I could and is just there to give a brief overview of the problem. Also, thanks for taking the time to consider this question!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This sounds to me like you have a process that is flawed at the start. Why would variables that should be there not be in the data set? Or possibly be named incorrectly?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also consider MERGE instead of SET for such a marginal purpose.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 19:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692676#M24934</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-19T19:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692681#M24936</link>
      <description>&lt;P&gt;The IF statement checks its condition in EVERY observation. Since only a THEN branch is supplied, only that action can be executed. The result of that execution (which happens once) is kept because of the automatic retain, caused by the resulting variable being present in an incoming dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 19:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692681#M24936</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-19T19:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692702#M24937</link>
      <description>&lt;P&gt;But why does that happen when the 2 datasets (test1 and test2) are set AND the if condition is used altogether in the same data step but the whole things works properly when we split the above code by first setting test1 and test2 in one dataset and then applying the IF condition in the new data set (that is, use 2 steps)? What I mean is why does the code&lt;/P&gt;&lt;P&gt;_________________________________&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;input visit $16.;&lt;BR /&gt;datalines;&lt;BR /&gt;First&lt;BR /&gt;Second&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data nulll;&lt;BR /&gt;input index;&lt;BR /&gt;datalines;&lt;BR /&gt;.&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;stop;&lt;BR /&gt;set nulll;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set test2 test1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Here is the difference - I've split the data step in the code into 2 data sets */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;if index(VISIT, 'Fir')^=0 then do;&lt;BR /&gt;index=100;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data=test;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;work as intended?&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 20:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692702#M24937</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-19T20:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692715#M24938</link>
      <description>&lt;P&gt;When you set both datasets,&amp;nbsp;&lt;EM&gt;and&lt;/EM&gt;&amp;nbsp;use the condition, the following happens:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;all variables are created in the PDV&lt;/LI&gt;
&lt;LI&gt;nothing is read into index, because the first dataset is empty, and the second dataset does not have the variable&lt;/LI&gt;
&lt;LI&gt;therefore, any value set once by the condition is retained&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If you do it in separate steps, in the second step&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;the variables are created as above&lt;/LI&gt;
&lt;LI&gt;but now index IS present in the incoming dataset (with missing values)&lt;/LI&gt;
&lt;LI&gt;therefore, the contents are overwritten with every observation read&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 20 Oct 2020 06:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692715#M24938</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-20T06:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692747#M24940</link>
      <description>&lt;P&gt;Thank You very much, much appreciated. One last question before I accept this as the as the solution - would you say that the second approach is better (i.e. setting the datasets and then using IF) when not using an ELSE statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, thanks! I'm sorry if I took too much out of your time.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 05:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692747#M24940</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-20T05:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692763#M24941</link>
      <description>&lt;P&gt;If you want something to happen when a condition is not met, then you &lt;EM&gt;have&lt;/EM&gt; to use the ELSE. Don't rely on "it's going to happen on its own". See Maxim 31.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whether you combine steps for efficiency, or keep them separate for coding clarity, is up to you.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 06:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692763#M24941</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-20T06:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692766#M24942</link>
      <description>Will have a look at the list for sure - I, however, assumed that if you don't enter an ELSE statement then the IF operator just evaluates the false statement as false, does nothing and moves on with the evaluation of the next row</description>
      <pubDate>Tue, 20 Oct 2020 07:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692766#M24942</guid>
      <dc:creator>teli4</dc:creator>
      <dc:date>2020-10-20T07:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Wrongfully assigning a value to a variable using "if"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692791#M24943</link>
      <description>&lt;P&gt;Before you wonder about the effects of a missing ELSE, you need to get familiar with the workings of the DATA step as such. When are variables set to missing, and which variables will be set to missing at that moment.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 08:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Wrongfully-assigning-a-value-to-a-variable-using-quot-if-quot/m-p/692791#M24943</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-20T08:55:09Z</dc:date>
    </item>
  </channel>
</rss>

