LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2009, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 11 May 2009 14:54:26 -0500
Reply-To:     Carl Denney <cdenney@HEALTHINFOTECHNICS.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Carl Denney <cdenney@HEALTHINFOTECHNICS.COM>
Subject:      Re: Reduce the length of a variable
Comments: To: "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
In-Reply-To:  <2210b143-b8fc-41ba-b22d-a3bda8fa9bba@s20g2000vbp.googlegro
              ups.com>
Content-Type: text/html; x-AttchRem=yes; charset="us-ascii"

<html> <body> <br> This might be useful for some of us.&nbsp; It's a little macro I wrote to shrink all the character variables to their minimum size. I got tired of importing a file and looking at it with fsview only to see one variable per screen.<br><br> It allows you to supply the dataset to shrink and guessingrows if the dataset is large. If the dataset has relatively few rows, you can use &quot;max&quot; for the guessingrows. You could also modify this to add a few characters to the length if you use guessingrows and think there might be some cases where the real length is over the maximum length found.<br><br> It also removes any format or informats so that Proc Fsview doesn't object afterward because the format is longer than the variable length.<br><br> The other structural difference is that all character variables are listed before all numeric variables.<br><br> <br> If SAS already has a function to do this, I will feel stupid but will appreciate it if someone will so inform me.<br><br> <br><br> <br> %macro sset(set2shrink,guessingrows);<br><br> /*<br> ** sset is short for shrinkset;<br> ** This macro looks at the length of all the character variables in a dataset and ;<br> ** shortens then to the length of the longest string in that field. This is useful;<br> ** when proc import brings in a dbf or access file with charvars set at $200;<br> */<br><br> &nbsp; proc contents noprint data=&amp;set2shrink out=var_property(where=(type=2));run;<br><br> &nbsp; proc sort;by varnum;run;<br><br> &nbsp; data _null_;<br> &nbsp; set var_property;<br> &nbsp; file 'c:\varlist.prc';<br> &nbsp; put '%shrinkvar(' name +(-1) ');';<br> &nbsp; run;<br><br> &nbsp; %let string= ;<br><br> &nbsp; %macro shrinkvar(varname);<br><br> &nbsp;&nbsp;&nbsp;&nbsp; proc sql;<br> &nbsp;&nbsp;&nbsp;&nbsp; create view test as<br> &nbsp;&nbsp;&nbsp;&nbsp; select length(&amp;varname) as varlen<br> &nbsp;&nbsp;&nbsp;&nbsp; from &amp;set2shrink.(obs=&amp;guessingrows)<br> &nbsp;&nbsp;&nbsp;&nbsp; ;quit;<br><br> &nbsp;&nbsp;&nbsp;&nbsp; proc univariate noprint data=test;<br> &nbsp;&nbsp;&nbsp;&nbsp; var varlen;<br> &nbsp;&nbsp;&nbsp;&nbsp; output out=result1 max=max;<br> &nbsp;&nbsp;&nbsp;&nbsp; run;<br><br> &nbsp;&nbsp;&nbsp;&nbsp; data _null_;<br> &nbsp;&nbsp;&nbsp;&nbsp; set result1;<br> &nbsp;&nbsp;&nbsp;&nbsp; new=&quot;&amp;varname&quot;||&quot; $&quot;||left(max);<br> &nbsp;&nbsp;&nbsp;&nbsp; call symput(&quot;new&quot;,new);<br> &nbsp;&nbsp;&nbsp;&nbsp; run;<br><br> &nbsp;&nbsp;&nbsp;&nbsp; %let string=&amp;string &amp;new;<br><br> &nbsp; %mend shrinkvar;<br> &nbsp; %inc 'c:\varlist.prc';<br><br> &nbsp; data &amp;set2shrink(compress=yes);<br> &nbsp; length &amp;string;<br> &nbsp; set &amp;set2shrink;<br> &nbsp; run;<br><br> &nbsp; data _null_;<br> &nbsp; lib=scan(&quot;&amp;set2shrink&quot;,1);<br> &nbsp; set=scan(&quot;&amp;set2shrink&quot;,2);<br> &nbsp;&nbsp;&nbsp; if set=' ' then do;set=lib;lib=&quot;work&quot;;end;<br> &nbsp; call symput(&quot;L&quot;,lib);<br> &nbsp; call symput(&quot;S&quot;,set);<br> &nbsp; run;<br><br> &nbsp; proc datasets NOLIST library=&amp;l; modify &amp;S; format _char_; informat _char_; run; quit;<br><br> %mend sset;<br><br> <br><br> <br><br> <br><br> At 02:09 PM 5/11/2009, Richard A. DeVenezia wrote:<br> <blockquote type=cite class=cite cite="">On May 11, 2:05 pm, Madhu G &lt;c2ma...@gmail.com&gt; wrote:<br> &gt; Hi All,<br> &gt;<br> &gt; can any body suggest me how can I reduce the size of the variable.<br> &gt;<br> &gt; In DI studio I need to map Name1(Char 50) to actual_name (char 30).<br> &gt;<br> &gt; when I am doing this it was saying target column length is smaller<br> &gt; than the source column length.<br> &gt;<br> &gt; and also<br> &gt;<br> &gt; one more error..<br> &gt;<br> &gt; ERROR: Variable has been created as Numeric as well as charecter.<br> &gt;<br> &gt; Can any body please suggest how can I overcome these two errors.<br><br> Try a SUBSTR transformation ?</blockquote> <x-sigsep><p></x-sigsep> <br> <b>Carl Denney, MSIE<br> <font size=2>Founder<br> Health InfoTechnics <br> 210 Jamestown Park, Suite 101, Brentwood, TN 37027<br> </font><font face="Lucida Console" size=2>615/298-4011</font> <br> <a href="http://www.healthinfotechnics.com/" eudora="autourl"> http://www.healthinfotechnics.com<br> </a></b></body> </html>


Back to: Top of message | Previous page | Main SAS-L page