[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: y=compress(x*1); ?
From:       S=?ISO-8859-1?Q?=C3=B8ren?= Lassen <s.lassen () POST ! TELE ! DK>
Date:       2012-11-29 12:32:19
Message-ID: 201211291232.qAT619Po030977 () waikiki ! cc ! uga ! edu
[Download RAW message or body]

Desiree,
You are right, there is not much difference. And yet, there are good reasons
to use INPUT.

The most important is readability. You yourself personally may know what
it is you are trying to do, but if somebody else is going to use/modify
your programs, the use of a function makes the purpose much clearer.
In my experience (about 30 years), much more time is spent reading and
modifying programs than actually writing programs. So you save a lot of
time in the long run if the purpose of the program is clear. And it may
also save some errors.

The second most important is flexibility - you may want to get rid of
all the error messages when X is not a number, to do that you can use
e.g. y=input(x,?? best32.) - the "??" will get rid of the error messages.
And you can write you own, very flexible formats, e.g.:
  proc format;
    invalue test
      low-high=[best32.]
      other=[date.]
     ;
  run;
  data a;
    length x $9;
    input x;
    y=input(x,test.);
  cards;
  01jan2011
  3425
  55e2
  ;run;

The third reason is performance. The implicit conversion is less efficient,
mostly because the SAS datastep compiler is not very much into optimizing.
When you write y=x*1, you can expect the compiler to actually instruct
the computer to
 1. convert x to a number using the default format
 2. check for correct conversion
 3. load the number 1 to a floating point register
 4. check for missing inputs to multiplication
 5. actually perform the multiplication
 6. load the result into Y
The INPUT function just does steps 1, 6 and possibly 2 (unless you use ??).
I know, you won't feel a thing unless you do it a a few million times,
so the performance consideration is not always important in our days.

Regards,
Søren

On Tue, 27 Nov 2012 10:48:34 -0500, Schaan, Desiree
<Desiree.Schaan@OCC.TREAS.GOV> wrote:

>Okay, thank you all for your comments.  I guess that I just don't like to
be insulted so early in the morning.
>
>I will still continue to use y=x*1, as I don't see any difference in the
results as compared to input.
>
>Jim and Joe, I see your points that there is a difference between the two
methods in terms of error messages.  And Jim, I also see your point that
the y=x*1 conversion will not correctly convert special missing values, but
I always know my data well enough to know whether someone has put special
missing value codes into a character variable.
>
>Thank you again!
>
>
>
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Jim
Groeneveld
>Sent: Tuesday, November 27, 2012 10:03 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: y=compress(x*1); ?
>
>Hi Desiree,
>
>Well, you have to know your data. Do not blindly trust that automatic
>conversion always yields the values that you expect. If in your expression
>x*1 x contains a value that unexpectedly can not be numerically recoded it
>causes a missing value and an ERROR flag in the log. You don't want that.
>Also consider the following example:
>
>DATA _NULL_;
>  LENGTH X $8;
>  x = '';
>  y = x*1;
>  z = INPUT (x, BEST12.);
>  PUT X= Y= Z=;
>  x = '.';
>  y = x*1;
>  z = INPUT (x, BEST12.);
>  PUT X= Y= Z=;
>  x = '._';
>  y = x*1;
>  z = INPUT (x, BEST12.);
>  PUT X= Y= Z=;
>RUN;
>
>As you can see with the third example (x=._, missing _) the automatically
>recoded value is incorrect.
>
>For that purpose I wrote a macro %aRecodeN that converts character values
>(of a list of variables) to numeric ones (using the INPUT function), while
>retaining its original name and variable label. With it you can specify
>character values that cannot be converted to a numerical equivalent and
>their desired numerical replacements (e.g. a value 'NA' might be replaced
>by the missing value .n or -1 or whatever.
>http://home.hccnet.nl/jim.groeneveld/software/SASmacro/aRecodeN.zip
>
>The PUT and INPUT functions are more efficient than automatic conversion
>according to the SAS documentation
>
(http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer
>.htm#a000780416.htm#a001304598)
>
>And you don't want those NOTEs in the log on automatic type conversion.
>
>See also all contributions on SAS-L on implicit type conversions:
>http://listserv.uga.edu/cgi-bin/wa?S2=sas-
>l&D=1&H=0&O=D&T=1&q=implicit+type+conversion&s=&f=&a=&b=
>
>Regards - Jim.
>--
>Jim Groeneveld, Netherlands
>Statistician/SAS consultant
>http://jim.groeneveld.eu.tf
>
>My job is to keep my computer busy
>and my computer's job is to keep me busy:
>http://jim.groeneveld.eu.tf/Various/News.txt
>
>
>On Tue, 27 Nov 2012 09:22:18 -0500, Schaan, Desiree
><Desiree.Schaan@OCC.TREAS.GOV> wrote:
>
>>What's wrong with using y=x*1 syntax where x is character and I want to
>convert to numeric?  I use it all the time, especially when converting
>character flag variables to numbers so that I can sum them up.  It works
>flawlessly, the code is short and simple, and I can always remember it.
>>
>>Why is this bad programming/or indication of laziness?
>>
>>-----Original Message-----
>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Nat
>Wooding
>>Sent: Thursday, November 22, 2012 1:44 PM
>>To: SAS-L@LISTSERV.UGA.EDU
>>Subject: Re: y=compress(x*1); ?
>>
>>Joe
>>
>>If I'm really being lazy and writing some ad hoc code, I may use the 1* to
>>do the conversion from character to numeric but the compress reverses the
>>process so can you think of any other language that such a construct would
>>make sense?
>>
>>Nat
>>
>>-----Original Message-----
>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Joe
>>Matise
>>Sent: Thursday, November 22, 2012 1:36 PM
>>To: SAS-L@LISTSERV.UGA.EDU
>>Subject: Re: y=compress(x*1); ?
>>
>>I agree with Jim and Nat.  I believe the X*1 syntax comes from other
>>languages, where it's the easiest way to convert character to numeric.
>> Even in those languages it's pretty bad programming style IMO, but it's
>>something I've seen before, particularly in SQL (perhaps from people who
>>aren't exactly programmers writing code).
>>-Joe
>>
>>
>>On Thu, Nov 22, 2012 at 5:45 AM, Roland Berry
>><rolandberry@hotmail.com>wrote:
>>
>>> I saw this in a sas program that I had never seen before:
>>>
>>> y=compress(x*1);
>>>
>>> "x" was a long text field and I think "y" was a text field. Why would
>>> anybody code this? What is it's purpose.
>>>
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic