<?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: If statement - SAS practice exam in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743769#M232938</link>
    <description>&lt;P&gt;Thank you very much. Left intending the datalines did the trick. I am running the code in SAS OnDemand. Has this always been the case? I was under the impression that SAS statements are independent of indentation.&lt;/P&gt;</description>
    <pubDate>Wed, 26 May 2021 08:04:05 GMT</pubDate>
    <dc:creator>YasodaJayaweera</dc:creator>
    <dc:date>2021-05-26T08:04:05Z</dc:date>
    <item>
      <title>If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743717#M232912</link>
      <description>&lt;P&gt;When I execute the following code, value of State is set to missing for each observation in the output dataset. Can someone please explain why the State is set to missing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I am expecting is as follows:&lt;/P&gt;&lt;P&gt;City State Region&lt;/P&gt;&lt;P&gt;Tulsa OK Western&lt;/P&gt;&lt;P&gt;Los Angales CA Western&lt;/P&gt;&lt;P&gt;Bangor &amp;lt;missing&amp;gt;&amp;nbsp; Western&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.GEO;
	infile datalines;
	input City $20.;
	if City='Tulsa' then 
	State = 'OK';
	Region='Central';
	if City='Los Angeles' then 
	State = 'CA';
	Region='Western'; 
	datalines;
	Tulsa
	Los Angeles
	Bangor
	;
run;
proc print data=WORK.GEO; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 22:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743717#M232912</guid>
      <dc:creator>YasodaJayaweera</dc:creator>
      <dc:date>2021-05-25T22:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743720#M232915</link>
      <description>&lt;P&gt;You have an indentation in your Datalines statement.&amp;nbsp; Typically, a Datalines statement must be left justified.&amp;nbsp; Try moving over all of your data&amp;nbsp; lines so that they are flush with the left boundary of the window and re-running.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 23:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743720#M232915</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-05-25T23:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743722#M232916</link>
      <description>&lt;P&gt;Show your LOG from running that code. Copy the code from the data step and all the messages for that step from the log, open a text box on the forum with the &amp;lt;/&amp;gt; icon and paste the text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I copied and ran what you show and get values of State for Tulsa and Los Angeles. Actually it matches your "expected" outcome. So I suspect your missing state will have something in the log to clarify possible causes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 23:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743722#M232916</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-25T23:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743724#M232917</link>
      <description>&lt;P&gt;I left justified the Datalines, but I suspect these results, see below, may not be what you want.&amp;nbsp; Remember, if you want multiple statements to be subject to the conditions of an IF statement, those statements must be grouped together inside of a DO;&amp;nbsp; ...statements...&amp;nbsp; END; construct.&amp;nbsp; Take a look below, and you'll see how I've modified your code, and below that you'll see the modified results.&amp;nbsp; I hope this is helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results as coded:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1621984787172.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59739i1C723101745F09B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1621984787172.png" alt="jimbarbour_0-1621984787172.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Modified code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.GEO;
	infile datalines;
	input City $20.;

	if City='Tulsa' then 
		DO;
			State = 'OK';
			Region='Central';
		END;
	ELSE
	if City='Los Angeles' then 
		DO;
			State = 'CA';
			Region='Western'; 
		END;
	ELSE
	if City='Bangor' then 
		DO;
			State = 'WA';
			Region='Western'; 
		END;

datalines;
Tulsa
Los Angeles
Bangor
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results from modified code:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_1-1621984893472.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59740i83949EA3FF374422/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_1-1621984893472.png" alt="jimbarbour_1-1621984893472.png" /&gt;&lt;/span&gt;&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>Tue, 25 May 2021 23:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743724#M232917</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-05-25T23:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743761#M232934</link>
      <description>The $20. informat will left hand justify whatever it finds automatically.  In fact, if you want to preserve leading blanks you would have to switch to the $char20. informat instead.&lt;BR /&gt;&lt;BR /&gt;Most likely you used the tab key when typing in the data, and tab characters distort the data values by becoming part of the data.</description>
      <pubDate>Wed, 26 May 2021 05:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743761#M232934</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-05-26T05:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743769#M232938</link>
      <description>&lt;P&gt;Thank you very much. Left intending the datalines did the trick. I am running the code in SAS OnDemand. Has this always been the case? I was under the impression that SAS statements are independent of indentation.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 08:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743769#M232938</guid>
      <dc:creator>YasodaJayaweera</dc:creator>
      <dc:date>2021-05-26T08:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743771#M232939</link>
      <description>&lt;PRE&gt; 
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         data WORK.GEO;
 74         infile datalines;
 75         input City $20.;
 76         putlog _all_;
 77         if City='Tulsa' then
 78         State = 'OK';
 79         Region='Central';
 80         putlog _all_;
 81         if City='Los Angeles' then
 82         State = 'CA';
 83         Region='Western';
 84         putlog _all_;
 85         datalines;
 
 City=Tulsa State=  Region=  _ERROR_=0 _N_=1
 City=Tulsa State=  Region=Central _ERROR_=0 _N_=1
 City=Tulsa State=  Region=Western _ERROR_=0 _N_=1
 City=Los Angeles State=  Region=  _ERROR_=0 _N_=2
 City=Los Angeles State=  Region=Central _ERROR_=0 _N_=2
 City=Los Angeles State=  Region=Western _ERROR_=0 _N_=2
 City=Bangor State=  Region=  _ERROR_=0 _N_=3
 City=Bangor State=  Region=Central _ERROR_=0 _N_=3
 City=Bangor State=  Region=Western _ERROR_=0 _N_=3
 NOTE: The data set WORK.GEO has 3 observations and 3 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.01 seconds
       system cpu time     0.00 seconds
       memory              677.09k
       OS Memory           30376.00k
       Timestamp           26/05/2021 08:04:26 AM
       Step Count                        31  Switch Count  2
       Page Faults                       0
       Page Reclaims                     144
       Page Swaps                        0
       Voluntary Context Switches        11
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 89         ;
 90         run;
 91         
 92         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 104        &lt;/PRE&gt;&lt;P&gt;I added few putlog statements to see the values. However, left indenting the data lines did the trick.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 08:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743771#M232939</guid>
      <dc:creator>YasodaJayaweera</dc:creator>
      <dc:date>2021-05-26T08:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743772#M232940</link>
      <description>Thank you for the reply. I was aiming for the first outcome purposely. The aim is to understand do-end should be used if multiple statements were to be executed for an if-then statement.</description>
      <pubDate>Wed, 26 May 2021 08:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/743772#M232940</guid>
      <dc:creator>YasodaJayaweera</dc:creator>
      <dc:date>2021-05-26T08:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/819364#M323432</link>
      <description>I came across the same question while preparing for base SAS. While the question here deals with something else, I was struggling to understand why all the values of var Region were Western. Thanks for the extra explanation!</description>
      <pubDate>Tue, 21 Jun 2022 16:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/819364#M323432</guid>
      <dc:creator>ShubhamMakar</dc:creator>
      <dc:date>2022-06-21T16:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: If statement - SAS practice exam</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/819375#M323436</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/329340"&gt;@YasodaJayaweera&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you very much. Left intending the datalines did the trick. I am running the code in SAS OnDemand. Has this always been the case? I was under the impression that SAS statements are independent of indentation.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS statements do not care about extra spaces between words in the code.&amp;nbsp; But you appear to be having issues with your lines of DATA, not your CODE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are using SAS OnDemand then you are using SAS/Studio.&amp;nbsp; Which means any TAB characters that have accidentally been inserted into your program will become part of the code and part of the data lines.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program editor will autoindent when you press enter to match the indentation of the previous line.&amp;nbsp; So before you type the DATALINES (or CARDS) statement make sure to tab back to the first column.&amp;nbsp; Then as you type the actual lines of data they will start in column 1 also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can tell SAS/Studio to convert the tabs into spaces so you don't get those tab characters inserted into your program files by changing your preferences to enable the &lt;STRONG&gt;Substitute spaces for tabs&lt;/STRONG&gt; option.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1655828651342.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72494iC9BF81F6147ECCCD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1655828651342.png" alt="Tom_0-1655828651342.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will prevent tabs from accidentally being added to your program files.&amp;nbsp; And since they are not part of the file then SAS/Studio cannot accidentally send them to SAS as part of the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also need to check how you are seeing REGION. Currently you are always setting REGION to western.&amp;nbsp; Indenting the Code properly can help with noticing that also.&lt;/P&gt;
&lt;P&gt;Either keep the IF/THEN statemen ton a single line.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.GEO;
  infile datalines;
  input City $20.;
  if City='Tulsa' then State = 'OK';
  Region='Central';
  if City='Los Angeles' then State = 'CA';
  Region='Western'; 
datalines;
Tulsa
Los Angeles
Bangor
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of if you want to split it to multiple lines then move the semicolon that marks the end to a new line.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.GEO;
  infile datalines;
  input City $20.;
  if City='Tulsa' then
    State = 'OK'
  ;
  Region='Central';
  if City='Los Angeles' then
   State = 'CA'
  ;
  Region='Western'; 
datalines;
Tulsa
Los Angeles
Bangor
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To have the REGION depend on the CITY then you might want to use DO/END block as the statement that the THEN condition runs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.GEO;
  infile datalines;
  input City $20.;
  if City='Tulsa' then do;
    State = 'OK';
    Region='Central';
  end;
  if City='Los Angeles' then do;
    State = 'CA';
    Region='Western'; 
  end;
datalines;
Tulsa
Los Angeles
Bangor
;
&lt;/CODE&gt;&lt;/PRE&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>Tue, 21 Jun 2022 16:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-SAS-practice-exam/m-p/819375#M323436</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-21T16:36:37Z</dc:date>
    </item>
  </channel>
</rss>

