1. 程式人生 > 其它 >SAS - 使用 put() 和 input() 轉換資料型別

SAS - 使用 put() 和 input() 轉換資料型別

使用 input () 還是 put() 轉換變數資料型別取決於 source data type 和 target data type。對於 input() 和 put(),有四條原則:

  • PUT() always creates character variables
  • INPUT() can create character or numeric variables based on the informat
  • The source format must match the source variable type in PUT()
  • The source variable type for INPUT() must always be character variables

EXAMPLE :

Convert Raw to Returned  Function Call  Raw Type  Raw Value

 Returned

Type

 Returned

Value

character variable to another character variable

A  PUT(name, $10.); char, char format ‘Richard’ char always ‘Richard   ’
numeric variable to character variable with numeric value B  PUT(age, 4.);
num, num format 30 char always ‘  30’
character variable to another character variable with a user defined format C  PUT(name, $nickname.); char, char format ‘Richard’ char always ‘Rick’
character variable with numeric value and informat to a numeric variable D  INPUT(agechar, 4.); char always ‘30’
num, num informat 30
character variable with numeric value and informat to a character variable E  INPUT(agechar, $4.); char always ‘30’ char, char informat ‘  30’
character variable with numeric value and informat to a numeric variable F  INPUT(cost,comma7.); char always ‘100,541’ num, num informat 100541