Hi all,
I know I've not that much experience, in the whole SAS environment and in SAS programming too but, since the time I started working with SAS EG projects, looking into SAS Community and in the net, I'm seeing a lot of questions (and great confusion, where I can also add mine) about "type casting", an area where, in other languages like C, there is no confusion at all: nearly all mature languages have their own typecast functions, explicit (java, C, etc.) or implicit (python) which "take care" to make the data having a "safe landing" into the target variables (or throw errors, if impossible or leads to data loss).
What I mean is: if some topic, main documentation apart, needs a so broad amount of "guides" and documents further explaining it (which I really never seen, in all my life, among other languages) there should be something wrong in the way it have been thought: apart the initial read I gave to the docs, I never needed to further read how to cast an int into a long, or have to take personally care about how var(string_value) decided to work on "3,14" to have 3.14 assigned to my num_short_pi numeric variable.
While using SAS I'm constantly banging my head against "typecast walls", all the time having to PUT something into my INPUT to have it converted etc. etc... so, after some time (as I initially thought it was my problem), I asked to myself "why not having an "intelligent typecast" function, which reads the source variable's type and, working behind the scenes, converts it into my target variable wanted data type?"
Thus this proposal: I know there is a CAST() function already but, from the few I read, it seems more related to proc sql (and even not all of it: works into FEDSQL), the proposal is to have it available "everywhere" (so even outside PROC SQL), in base SAS, as an alternative to PUT/INPUT. Then, with time, if SAS devs will see users using it more than original PUT/INPUT, they could decide to keep CAST only... or to remove it if, to the contrary, it won't be used at all.
Syntax: CAST(source variable, target variable, target variable desired type, [target variable format]);
source variable: any type (numeric or char) source variable name
target variable: target variable name
target variable desired type: wanted target type (numeric or char), if not defined before
(optional) target variable format: the target format to apply to target variable desired type
As I'm not a SAS expert, even if I believe there shouldn't be problems in converting char-to-num or num-to-char, some problems could arise: this is why I wrote "target variable desired type": if desired type can't be achieved, e.g. there is data loss in the conversion, a warning should be raised, if the conversion can't take place at all, an error should be raised.
As I told, I'm no expert, so feel free to improve this proposal with your expertise, which it's for sure far greather than mine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.