DATA Step, Macro, Functions and more

Character variable format association without dollar sign.

Reply
Respected Advisor
Posts: 3,852

Character variable format association without dollar sign.

It's Friday.

 

proc format;
   value xfmt  other='Num';
   value $xfmt other='Chr';
   run;
data class;
   set sashelp.class;
   format _all_ xfmt.;
   run;
proc contents varnum;
   run;
      Variables in Creation Order

#    Variable    Type    Len    Format

1    Name        Char      8    $XFMT.
2    Sex         Char      1    $XFMT.
3    Age         Num       8    XFMT.
4    Height      Num       8    XFMT.
5    Weight      Num       8    XFMT.

 

27   data class;
28      set sashelp.class;
29      format _all_ xfmt.;
30      run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
Trusted Advisor
Posts: 1,256

Re: Character variable format association without dollar sign.

Posted in reply to data_null__

This is good to know. Thanks for sharing! It simplifies the assignment of two of my favorite user-defined formats:

proc format;
value miss
._-.z = 'missing'
other = 'non-missing';

value $miss
' '   = 'missing'
other = 'non-missing';
run;

 

Super User
Posts: 10,784

Re: Character variable format association without dollar sign.

Posted in reply to data_null__

Very interesting thing.

Maybe SAS is too smart . a.k.a AI technology .

Respected Advisor
Posts: 3,852

Re: Character variable format association without dollar sign.

Posted in reply to data_null__

Note this trick doesn't work in PROCs at least not SUMMARY/MEANS my favorite.  I use a data step VIEW.

 

My application is passed a list of variables of mixed type.  I want to associated a format to collapse all non-missing levels to one similar to @FreelanceReinhard missing/not-missing example.  I "can't" use _NUMERIC_ or _CHARACTER_ because I have other variable(s) that I don't want to associated with the format.

 

This also works but changes the order of the variables in the data set.  Order in the is usually not a concern.

 

proc format;
   value xfmt  other='Num';
   value $xfmt other='Chr';
   run;

%let vlist=Status DeathCause AgeCHDdiag Sex;

data heart;
   set sashelp.heart(keep=&vlist);
   format _character_ $xfmt. _numeric_ xfmt.;
   set sashelp.heart(drop=&vlist);
   run;
proc contents varnum;
   run;

  #    Variable          Type    Len    Format    Label

  1    Status            Char      5    $XFMT.
  2    DeathCause        Char     26    $XFMT.    Cause of Death
  3    AgeCHDdiag        Num       8    XFMT.     Age CHD Diagnosed
  4    Sex               Char      6    $XFMT.
  5    AgeAtStart        Num       8              Age at Start
  6    Height            Num       8
  7    Weight            Num       8
  8    Diastolic         Num       8
  9    Systolic          Num       8
 10    MRW               Num       8              Metropolitan Relative
 11    Smoking           Num       8
 12    AgeAtDeath        Num       8              Age at Death
 13    Cholesterol       Num       8
 14    Chol_Status       Char     10              Cholesterol Status
 15    BP_Status         Char      7              Blood Pressure Status
 16    Weight_Status     Char     11              Weight Status
 17    Smoking_Status    Char     17              Smoking Status
Ask a Question
Discussion stats
  • 3 replies
  • 214 views
  • 7 likes
  • 3 in conversation