DATA Step, Macro, Functions and more

Why Can I Use _CHARACTER_ as a Variable Name?

Reply
Regular Contributor
Posts: 184

Why Can I Use _CHARACTER_ as a Variable Name?

Consider:

[pre]
335 data _null_ ;
336 _all_ = 123 ;
ERROR: Cannot use _all_ as a variable name.
337 _numeric_ = 456 ;
ERROR: Cannot use _numeric_ as a variable name.
338 _character_ = 789 ;
339 run ;
[/pre]

There's no ERROR after line 338. _CHARACTER_ can be used as a variable name (not that this would be a good idea).

Can anyone provide a good reason for the asymmetry with _NUMERIC_ and _ALL_, which trigger errors when used as variable names?

SAS 9.2 2M3 on Win XP.

Thanks.
Respected Advisor
Posts: 3,799

Re: Why Can I Use _CHARACTER_ as a Variable Name?

I cannot answer the question but though this was interesting.

[pre]
2326 data _null_;
2327 _char_ = 1233;
ERROR: Cannot use _char_ as a variable name.
2328 run;
[/pre]
Trusted Advisor
Posts: 1,321

Re: Why Can I Use _CHARACTER_ as a Variable Name?

This is a Special SAS Name List (a type of SAS Variable List) and as such is a reserved word...

Check out doco - http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695105.htm

Hope this helps,
Michelle
Super Contributor
Super Contributor
Posts: 3,174

Re: Why Can I Use _CHARACTER_ as a Variable Name?

Posted in reply to MichelleHomes
To the OP: if you want to pursue this type of anomaly, you will be best served by going directly to SAS Tech Support and opening a track to ask the question, given what you have uncovered.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,177

Re: Why Can I Use _CHARACTER_ as a Variable Name?

Posted in reply to MichelleHomes
Michelle
I can understand why _character_ is reserved, but cannot find reference at the link you provided (nor elsewhere) to _char_

Is it just an undocumented alternative to _character_ ?

peterC
Respected Advisor
Posts: 3,799

Re: Why Can I Use _CHARACTER_ as a Variable Name?

> Is it just an undocumented alternative to _character_
> ?

I thought _CHAR_was documented but cannot find documentation in relation to SAS Variable List used in data/proc steps.

It would appear to me that the "data step team" at SAS thinks _CHAR_ is "something".

Perhaps _CHARACTER_ is allowed to provide some backward compatibility from days of old.

Further fiddling with _CHARACTER_ produced interesting context sensitive results.

[pre]
2614 options stimer=0 fullstimer=0 missing=.;
2615 data test(keep=_character_);
2616 if 0 then set sashelp.class;
2617 *_char_ = 1233;
2618 _character_ = '9';
2619 cm = cmiss(of _character_);
2620 put _character_;
2621 put (_character_)(=);
2622 put _character_=;
2623 *_numeric_ = '0';
2624 array c
  • _character_;
    2625 put (c
  • ) (=);
    2626 put _all_;
    2627 output;
    2628 stop;
    2629 run;

    9
    Name= Sex= _character_=9
    _character_=9
    Name= Sex= _character_=9
    Name= Sex= Age=. Height=. Weight=. _character_=9 cm=2 _ERROR_=0 _N_=1
    NOTE: The data set WORK.TEST has 1 observations and 3 variables.

    2630 data _null_;
    2631 set test;
    2632 put _all_;
    2633 run;

    Name= Sex= _character_=9 _ERROR_=0 _N_=1
    [/pre]
  • Valued Guide
    Posts: 2,177

    Re: Why Can I Use _CHARACTER_ as a Variable Name?

    Posted in reply to data_null__
    Stunning

    _char_ is not valid as a var name
    It has the effect of _character_ when used in a keep list
    I found no corresponding abbreviation of _numeric_
    Regular Contributor
    Posts: 184

    Re: Why Can I Use _CHARACTER_ as a Variable Name?

    We don't seem to have a solid explanation, so I guess it's time to put this in front of Tech Support.

    The point is that _CHARACTER_ is *not* reserved, at least not in the way _ALL_ is.

    Also notice that enforcement is more or less limited to the DATA step. Here is a PROC step which is happy to permit use of variable names which are not allowed (or at least discouraged) in the DATA step.

    [pre]
    proc summary data=sashelp.class(obs=3) nway ;
    class sex ;
    output out=badnames(rename = (_freq_ = _n_ _type_=_error_) )
    min(age) = _numeric_
    max(age) = _character_
    mode(age)= _all_
    ;
    run ;
    [/pre]

    Of course referencing these names will be a problem.

    Another slightly interesting bit of evidence. When this code

    [pre]
    proc print ;
    var _numeric_ _character_ _char_ _all_ _notmetoo_ ;
    run ;
    [/pre]

    is placed in the enhanced editor, the first four specs in the VAR statement are recognized and thus colored blue rather than black.
    Ask a Question
    Discussion stats
    • 7 replies
    • 890 views
    • 0 likes
    • 5 in conversation