<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/use-the-length-statement-to-pre-set-the-lengths-of-character-variables-in-sas-with-a-comparison-to-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chemicalstatistician.wordpress.com/2017/08/16/use-the-length-statement-to-pre-set-the-lengths-of-character-variables-in-sas/"> R programming – The Chemical Statistician</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I often create character variables (i.e. variables with strings of text as their values) in SAS, and they sometimes don’t render as expected.  Here is an example involving the built-in data set SASHELP.CLASS.</p>
<p>Here is the code:</p>
<pre>data c1;
     set sashelp.class;
 
     * define a new character variable to classify someone as tall or short;
     if height > 60
     then height_class = 'Tall';
          else height_class = 'Short';
run;


* print the results for the first 5 rows;
proc print
     data = c1 (obs = 5);
run;</pre>
<p>Here is the result:</p>
<div>
<div>
<div align="center">
<table summary="Procedure Print: Data Set WORK.C1" cellspacing="0" rules="all" cellpadding="5">
<thead>
<tr>
<th scope="col">Obs</th>
<th scope="col">Name</th>
<th scope="col">Sex</th>
<th scope="col">Age</th>
<th scope="col">Height</th>
<th scope="col">Weight</th>
<th scope="col">height_class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Alfred</td>
<td>M</td>
<td>14</td>
<td>69.0</td>
<td>112.5</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Alice</td>
<td>F</td>
<td>13</td>
<td>56.5</td>
<td>84.0</td>
<td>Shor</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Barbara</td>
<td>F</td>
<td>13</td>
<td>65.3</td>
<td>98.0</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Carol</td>
<td>F</td>
<td>14</td>
<td>62.8</td>
<td>102.5</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">5</th>
<td>Henry</td>
<td>M</td>
<td>14</td>
<td>63.5</td>
<td>102.5</td>
<td>Tall</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>What happened?  Why does the word “Short” render as “Shor”?</p>
<p><span></span></p>
<p>This occurred because SAS sets the length of a new character variable as the length of the first value given in its definition.  My code defined “height_class” by setting the value “Tall” first, which has a length of 4.  Thus, “height_class” was defined as a character variable with a length of 4.  Any subsequent values must follow this variable type and format.</p>
<p>How can we circumvent this?  You can pre-set the length of any new variable with the LENGTH statement before the SET statement.  In the revised code below, I correct the problem by setting the length of “height_class” to 5 before defining its possible values.</p>
<pre>data c2;
     set sashelp.class;
 
     * define a new character variable to classify someone as tall or short;
     <strong>length height_class $ 5;</strong>
     if height > 60
     then height_class = 'Tall';
          else height_class = 'Short';
run;


* print the results for the first 5 rows;
proc print
     data = c2 (obs = 5);
run;</pre>
<p>Here is the result:</p>
<div>
<div>
<div align="center">
<table summary="Procedure Print: Data Set WORK.C1" cellspacing="0" rules="all" cellpadding="5">
<thead>
<tr>
<th scope="col">Obs</th>
<th scope="col">Name</th>
<th scope="col">Sex</th>
<th scope="col">Age</th>
<th scope="col">Height</th>
<th scope="col">Weight</th>
<th scope="col">height_class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Alfred</td>
<td>M</td>
<td>14</td>
<td>69.0</td>
<td>112.5</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Alice</td>
<td>F</td>
<td>13</td>
<td>56.5</td>
<td>84.0</td>
<td>Short</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Barbara</td>
<td>F</td>
<td>13</td>
<td>65.3</td>
<td>98.0</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Carol</td>
<td>F</td>
<td>14</td>
<td>62.8</td>
<td>102.5</td>
<td>Tall</td>
</tr>
<tr>
<th scope="row">5</th>
<td>Henry</td>
<td>M</td>
<td>14</td>
<td>63.5</td>
<td>102.5</td>
<td>Tall</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p> </p>
<p>Notice that “height_class” for Alice is “Short”, as it should be.</p>
<p>An alternative solution is to re-write the code so that the first instance of “height_class” is the longest possible value.  This does not require the use of the LENGTH statement.</p>
<pre>data c3;
     set sashelp.class;
 
     * define a new character variable to classify someone as tall or short;
     if height &lt; 60
          then height_class = 'Short';
     else height_class = 'Tall';
run;</pre>
<p> </p>
<p>By the way, I don’t notice this problem in R.  Here is some code to illustrate this observation.</p>
<pre><span>> </span><span>set.seed(235)
</span><span>> </span>
<span>> </span><span># randomly generate 4 values
</span><span>> </span><span>x = rnorm(3, 60, 5)
</span><span>> </span>
<span>> </span><span># add a value to the beginning of &quot;x&quot; so that the first value is above 60
</span><span>> </span><span># add a value to the end of &quot;x&quot; so that the last vlaue is below 60
</span><span>> </span><span>x = c(63, x, 57)
</span><span>> </span><span>x
</span>[1] 63.00000 70.68902 61.36082 56.62601 57.00000
<span>> </span>
<span>> </span><span># pre-allocate a vector for classifying &quot;x&quot; as &quot;tall&quot; or &quot;short&quot;
</span><span>> </span><span>y = 0 * x
</span><span>> </span>
<span>> </span>
<span>> </span><span>for (i in 1:length(x))
</span><span>+ </span><span>{
</span><span>+ </span><span> if (x[i] > 60)
</span><span>+ </span><span> {
</span><span>+ </span><span> y[i] = 'Tall'
</span><span>+ </span><span> }
</span><span>+ </span><span> else
</span><span>+ </span><span>{
</span><span>+ </span><span> y[i] = 'Short'
</span><span>+ </span><span> }
</span><span>+ </span><span>}
</span><span>> </span>
<span>> </span>
<span>> </span><span># display &quot;y&quot;
</span><span>> </span><span>y
</span>[1] &quot;Tall&quot;  &quot;Tall&quot;  &quot;Tall&quot;  &quot;Short&quot; &quot;Short&quot;</pre>
<p>Notice that the value “Short” renders fully with a length of 5.  I did not need to pre-set the length of “y” first.</p>
<p>Filed under: <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/categorical-data-analysis/" target="_blank">Categorical Data Analysis</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/data-analysis-2/" target="_blank">Data Analysis</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/r-programming/" target="_blank">R programming</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/sas-programming/" target="_blank">SAS Programming</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/statistics-2/" target="_blank">Statistics</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/category/tutorials/" target="_blank">Tutorials</a> Tagged: <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/categorical-data/" target="_blank">categorical data</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/categorical-variable/" target="_blank">categorical variable</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/character-data/" target="_blank">character data</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/character-variable/" target="_blank">character variable</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/length/" target="_blank">length()</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/r/" target="_blank">R</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/r-programing/" target="_blank">r programing</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/sas/" target="_blank">SAS</a>, <a rel="nofollow" href="https://chemicalstatistician.wordpress.com/tag/sas-programming-2/" target="_blank">sas programming</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/delicious/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/facebook/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/twitter/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/stumble/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/digg/chemicalstatistician.wordpress.com/3713/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chemicalstatistician.wordpress.com/3713/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/reddit/chemicalstatistician.wordpress.com/3713/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://chemicalstatistician.wordpress.com/2017/08/16/use-the-length-statement-to-pre-set-the-lengths-of-character-variables-in-sas/"> R programming – The Chemical Statistician</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hVB8pSfprAg" height="1"></a></div>