<?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 string variables and location of data values in the variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971047#M377244</link>
    <description>&lt;P&gt;Trivial question that i don't understand. The documentation for a data file i'm preparing specifies that subject_id be a 20 character string (text) variable. The values that i am loading into that variable are 3 digit numbers. I do this:&amp;nbsp;subject_id=put(record_id, 20.);. when i examine the dataset in viewtable, data values appear to be maybe somewhat centered in that 20 character field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am also required to define a 20 character text variable for sex and load single character data values (M or F). I do this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;length sex $ 20;&lt;BR /&gt;sex=tsex;&lt;/P&gt;&lt;P&gt;Values for this variable display against the left field boundary, i.e., character column 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then read this file, after saving it, into spss and see that subject_id data values display in columns 17-20 and sex data values display in columns 1-3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps it doesn't matter but I'd like to understand why the differences and how to make them similar.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, Gene Maguin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Jul 2025 20:16:10 GMT</pubDate>
    <dc:creator>emaguin</dc:creator>
    <dc:date>2025-07-17T20:16:10Z</dc:date>
    <item>
      <title>string variables and location of data values in the variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971047#M377244</link>
      <description>&lt;P&gt;Trivial question that i don't understand. The documentation for a data file i'm preparing specifies that subject_id be a 20 character string (text) variable. The values that i am loading into that variable are 3 digit numbers. I do this:&amp;nbsp;subject_id=put(record_id, 20.);. when i examine the dataset in viewtable, data values appear to be maybe somewhat centered in that 20 character field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am also required to define a 20 character text variable for sex and load single character data values (M or F). I do this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;length sex $ 20;&lt;BR /&gt;sex=tsex;&lt;/P&gt;&lt;P&gt;Values for this variable display against the left field boundary, i.e., character column 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then read this file, after saving it, into spss and see that subject_id data values display in columns 17-20 and sex data values display in columns 1-3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps it doesn't matter but I'd like to understand why the differences and how to make them similar.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, Gene Maguin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jul 2025 20:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971047#M377244</guid>
      <dc:creator>emaguin</dc:creator>
      <dc:date>2025-07-17T20:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: string variables and location of data values in the variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971050#M377245</link>
      <description>&lt;P&gt;Leading blanks definitely matter.&amp;nbsp; : )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use the PUT function to convert a numeric value to character and specify the format as 20. you are asking SAS to give you have value that is 20 characters long.&amp;nbsp; So if you have 3 digit number,&amp;nbsp; you will have 17 blanks followed by 3 digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS, leading blanks can cause lots of confusion, because they are part of the value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  record_id=123 ;
  subject_id=put(record_id,20.) ;
  if subject_id='123' then put "Not found! ";
  if subject_id='                 123' then put "leading blanks!" ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can add the -l&amp;nbsp; (that's l for left) option on the format to left-align the value:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a ;
  record_id=123 ;
  subject_id=put(record_id,20. -l) ;
  if subject_id='123' then put "no leading blanks when you use -l" ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jul 2025 20:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971050#M377245</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-07-17T20:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: string variables and location of data values in the variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971062#M377247</link>
      <description>&lt;P&gt;Don't look at printouts made using ODS as it will distort things. In addition to using proportional fonts it will also remove leading spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely the difference is because TSEX as already character, and so did not have any leading spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The numeric format, such as the 20. you used, will right align the values.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unless there is some need to display the numbers in a particular style (perhaps the numbers are DATE values for example) then I would just use the CATS() function instead of PUT().&amp;nbsp; That also has the advantage of working the same with either numbers or strings as input.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length subject_id sex $20 ;
subject_id=cats(record_id);
sex=cats(tsex);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jul 2025 01:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-and-location-of-data-values-in-the-variable/m-p/971062#M377247</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-18T01:17:45Z</dc:date>
    </item>
  </channel>
</rss>

