<?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: Do FORMAT come before or after INPUT statement? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837498#M331139</link>
    <description>&lt;P&gt;thank you Tom for the detailed reply, I will learn about the LENGTH statement.&lt;/P&gt;</description>
    <pubDate>Sat, 08 Oct 2022 04:55:02 GMT</pubDate>
    <dc:creator>Nietzsche</dc:creator>
    <dc:date>2022-10-08T04:55:02Z</dc:date>
    <item>
      <title>Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837474#M331120</link>
      <description>&lt;P class=""&gt;Hi, I am currently reading SAS Essential book by Elliot.&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;In one example (page 77) the FORMAT statement comes &lt;STRONG&gt;after&lt;/STRONG&gt; the INPUT statement.&lt;/P&gt;&lt;PRE&gt;DATA MYDATA;
INPUT @1 FNAME $11. @12 LNAME $12. @24 BDATE DATE9.;
FORMAT BDATE WORDDATE12.;
LABEL
FNAME="First Name"
LNAME="Last Name"
BDATE="Birth Date"; 

DATALINES;
Bill       Smith       08JAN1952
Jane       Jones       02FEB1953
Clyde      York        23MAR1949
;
PROC PRINT LABEL;
RUN;&lt;/PRE&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if I change the FORMAT to before the INPUT statement, I will move the BDATE variable to column 1 instead of column 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;Then in another example (page 79), the FORMAT statement comes &lt;STRONG&gt;before&lt;/STRONG&gt; the INPUT statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data roomsize;

format room $10.;
input room $ W L;
area = L*W;

label L = "Length" W= "Width" Area = "Sq. Feet";

datalines;
LIVING 14 22
DINING 14 12
BREAKFAST 10 12
KITCHEN 12 16
BEDROOM1 18 12
BEDROOM2 12 14
BEDROOM3 13 16
BATH1 8 12
BATH2 7 10
BATH3 6 8
GARAGE 23 24; run;

proc print label;
sum area; run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;If I change the FORMAT to after the INPUT statement, the room variable names will not be formatted.&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;So why questions is what is rule regarding to the location of the FORMAT statement in SAS? The book does not really explain it and the order of the FORMAT and the INPUT statement obvious have a visible effect on the result.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 22:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837474#M331120</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-10-07T22:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837485#M331128</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I am not familiar with the book you're referencing. However, I fear that you are misunderstanding the purpose of the FORMAT statement. SAS builds the descriptor portion of the data set based on the references it finds in the program, working from top to bottom in the code during a "pre-processing" phase called compile time. The FORMAT statement in this program is ONLY impacting the order of variables as they are internally stored in the SAS data set that you are creating. Typically, the "housekeeping" statements like the FORMAT statement are either placed at the very top of the SAS program or the very bottom of the SAS program (in this case, before the DATALINES statement). But the internally stored order of the variables really doesn't matter until you run a PROC PRINT taking all the defaults. The default order that variables are displayed in PROC PRINT is the internally stored order of the variables. The reason you need the LABEL option in PROC PRINT is that the default for PROC PRINT is NOT to use the labels that you specify. So you are already overriding one of the PROC PRINT defaults. Why not learn about the VAR statement and then you don't have to worry about the placement of the FORMAT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; When the FORMAT statement for BDATE is before the INPUT statement in the program, then the BDATE variable would be listed and defined first in the descriptor portion of the data set. So if you have a PROC PRINT such as you show, when the FORMAT statement comes first, then you would probably see BDATE listed first in any PROC PRINT output. This is not something to stress about. You can display the variables in any order you want using a VAR statement in PROC PRINT. The internal storage order of the variables really is not something to worry about since it is easy to control. Here's the default ordering when you have the FORMAT statement after the INPUT statement:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1665189552032.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76023iBB59A086D6405F5B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1665189552032.png" alt="Cynthia_sas_1-1665189552032.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here's the ordering when you have the FORMAT statement first, but as you can see, the VAR statement allows you to control the order of the variables:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1665189414975.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76022i82D531086AC48765/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1665189414975.png" alt="Cynthia_sas_0-1665189414975.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I hope this explains what I think you're seeing. And, yes, I changed the data in your program for my screen shots because I was curious to see whether there was a first name that was 11 characters long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 00:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837485#M331128</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2022-10-08T00:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837487#M331130</link>
      <description>&lt;P&gt;In your first code sample, you note that putting the FORMAT statement prior to the INPUT statement makes the formatted variable as column 1.&amp;nbsp; That's because, no matter what order the source data is physically read, the SAS compiler (which prepares the subsequent actual executable computer instructions) will make provision for variables in the order they are mentioned in your SAS program code.&amp;nbsp; Moving the FORMAT prior to the INPUT statement means variable BDATE is mentioned prior to all the other variables, and seen first by the compiler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the second sample, what do you mean by "&lt;SPAN&gt;If I change the FORMAT to after the INPUT statement, the room variable names will not be formatted.".&amp;nbsp; &amp;nbsp;I suspect you mean that that variable &lt;EM&gt;&lt;STRONG&gt;room&lt;/STRONG&gt;&lt;/EM&gt; is truncated from what you expected, so BREAKFAST is missing the last two characters.&amp;nbsp; But if you do a proc contents on dataset ROOMSIZE, you will see that room &lt;EM&gt;&lt;STRONG&gt;actually is&lt;/STRONG&gt; &lt;STRONG&gt;formatted&lt;/STRONG&gt;&lt;/EM&gt; to $10, but its &lt;EM&gt;&lt;STRONG&gt;storage length&lt;/STRONG&gt;&lt;/EM&gt; is only $8.&amp;nbsp; That's because when the INPUT statement was encountered, SAS compiler assigned a default length of $8, because the compiler had not yet seen the subsequent FORMAT $10. statement implying a length of 10 bytes.&amp;nbsp; Of course, you could leave the FORMAT to follow the input statement, if you inserted a LENGTH ROOM $10; statement prior to INPUT.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 04:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837487#M331130</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-10-08T04:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837490#M331132</link>
      <description>&lt;P&gt;&lt;STRONG&gt;The first place you reference the variable is when it is added to the dataset.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A FORMAT statement has NOTHING to with defining the variable. The purpose of a FORMAT statement is to tell SAS how you want the variable to DISPLAY.&amp;nbsp; Note that most variables do not need to have special formats attached to them.&amp;nbsp; DATE, TIME and DATETIME variables are examples of variables where attaching a format is important as the raw numbers will be hard for humans to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;You can use the LENGTH statement, or ATTRIB statement with the LENGTH= option, to explicitly tell SAS how you want the variables defined. &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without that SAS will have to &lt;STRONG&gt;GUESS&lt;/STRONG&gt; how you wanted to define the variable.&amp;nbsp; It will use clues from how you referenced the variable to guide its guess.&amp;nbsp; Like did you use it with a format, perhaps in a FORMAT statement or an INPUT statement?&amp;nbsp; If so it will guess that you wanted the type to match the type of the format.&amp;nbsp; For character variables it will guess that you wanted the length to match the display width of the format (which is not always a good idea).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't give it any information about how long the character variable should be it will default it to a length of 8 bytes.&amp;nbsp; Like in this INPUT statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input room $ W L;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that is the first place is sees those three variables then ROOM will be defined as character with a length of 8 and and W and L will be defined as numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;If you have properly defined the variables first then it does not matter whether you place the FORMAT statement of the INPUT statement first.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your programs should look something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
  length fname $11 lname $12 bdate 8 ;
  input fname 1-11 lname 12-23 bdate date9.;
  format bdate worddate12.;
  label
    fname="First Name"
    lname="Last Name"
    bdate="Birth Date"
  ;
datalines;
Bill       Smith       08JAN1952
Jane       Jones       02FEB1953
Clyde      York        23MAR1949
;

data roomsize;
  length room $10 w l area 8;
  input room W L;
  area = L*W;
  label L = "Length" W= "Width" Area = "Sq. Feet";
datalines;
LIVING 14 22
DINING 14 12
BREAKFAST 10 12
KITCHEN 12 16
BEDROOM1 18 12
BEDROOM2 12 14
BEDROOM3 13 16
BATH1 8 12
BATH2 7 10
BATH3 6 8
GARAGE 23 24
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 02:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837490#M331132</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-08T02:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837496#M331137</link>
      <description>&lt;P&gt;thank you Cynthia for the detailed reply&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 04:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837496#M331137</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-10-08T04:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837497#M331138</link>
      <description>&lt;P&gt;thank you mketintz for the reply, I think your answer is the best in answering what I am seeking.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 04:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837497#M331138</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-10-08T04:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Do FORMAT come before or after INPUT statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837498#M331139</link>
      <description>&lt;P&gt;thank you Tom for the detailed reply, I will learn about the LENGTH statement.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2022 04:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-FORMAT-come-before-or-after-INPUT-statement/m-p/837498#M331139</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-10-08T04:55:02Z</dc:date>
    </item>
  </channel>
</rss>

