[SAS Formats]ERROR: For format COUNTRIES, this ran

2019-12-16 20:07发布

问题:

I simply tried defining a new format on SAS studio. The following is my code and the error msg is as in the title. Can anyone help? Thanks a lot!

data countries;
    input Start $  Label $;
    retain FmtName 'countries';
    datalines;
    'AU' 'Austr'
    'CH' 'China'
;
proc format library=orion.MyMacFmts1 fmtlib cntlin=countries;
    select $countries;
run;

回答1:

You have two different problems in your code:

  1. Your data values have quotes in them
  2. SAS is trying to build a numeric format, not a character format

The quote marks in your datalines section are becoming part of the values of the variable, which you probably don't intend. You should remove them, i.e.:

data countries;
    input Start $  Label $;
    retain FmtName '$countries';  *<-- added $ sign! 
    datalines;
AU Austr
CH China
;

As written, your PROC FORMAT statement is creating a numeric format, Countries. SAS tries to convert the character values to numeric, and you get null values (that repeat, thus the error message). To tell SAS you want a character format, you add a $ prefix to the name (the $ is actually part of the format name). You can also add a TYPE variable to the dataset.

Then your code works:

proc format library=work.formats fmtlib cntlin=countries;
    select $countries;
run;

The fmtlib option and select statement simply tell SAS to print the format (which is helpful for debugging sometimes). They are optional, so this will work:

proc format library=work.formats cntlin=countries;
run;

Test like:

307  data _null_;
308    length Country $8;
309    do country="AU","CH","USA";
310      put country country $countries.;
311    end;
312  run;

AU Austr
CH China
USA USA


标签: sas