בשבועות האחרונים ראינו איך PROC FORMAT מאפשר לנו לייצור בעצמו פורמטים, כלומר מיפוי בין ערכים. למעשה, בעזרת PROC FORMAT אנחנו יכולים ליצור שלושה סוגים שונים של תרגומים שכאלו: מיפוי ממספר למחרוזת, מיפוי ממחרוזת למחרוזת ומיפוי ממחרוזת למספר. עקרונית, ניתן גם לייצור בעזרת PROC FORMAT מיפוי ממספר למספר אבל נראה בהמשך ש - SAS מאחורי הקלעים למעשה הופכת את המספר שאנחנו רוצים להמיר למחרוזת ואז מפעילה עליה את הפורמט ככה שלמעשה לא מדובר בהמרה ישירה ממספר למספר.
כחלק מבניית הפורמט שלנו אנחנו צריכים לרמוז ל – PROC FORMAT איזה סוג המרה אנחנו מנסים לעשות בשני מקומות: שימוש בפקודת value או invalueבתוך ה – PROC FORMAT וע"י הוספת הרמז לשם הפורמט:
* Numeric to Character;
proc format;
value age_group
low-12='A'
13-high='B';
run;
data NUM2CHAR;
set SASHELP.CLASS;
length Formatted $1;
Formatted=put(age, age_group.);
run;
* Character to Character;
proc format;
value $sex_desc /*Note the dollar sign before the name of the format*/
'M'='Male'
'F'='Female';
run;
data CHAR2CHAR;
set SASHELP.CLASS;
length Formatted $6;
Formatted=put(sex, sex_desc.);
run;
* Character to Numeric;
proc format;
invalue sex_order /*Note using INVALUE instead of VALUE statement*/
'M'=2
'F'=1;
run;
data CHAR2NUM;
set SASHELP.CLASS;
length Formatted 8;
Formatted=input(sex, sex_order.); * Note the INPUT function instead of the PUT function;
run;
* Numeric to Numeric;
proc format;
invalue age_group_num
low-12=1
13-high=2;
run;
data NUM2NUM;
set SASHELP.CLASS;
length Formatted 8;
Formatted=input(age, age_group_num.); * Notice the note SAS wrote to the log;
run;
/*
Actually, behind the scenes, SAS converts the AGE variable to a string and then apply our informat on it.
To prevent this note in the log you need to run the following code:
*/
data NUM2NUM;
set SASHELP.CLASS;
length Formatted 8;
Formatted=input(put(age, 2.), age_group_num.); * Converting AGE to a string and then applying the informat. No note in the log;
run;
לסיום הערה קטנה לפדנטים. טכנית תרגום מחרוזת למספר מכונה ב – SAS בשם informat ואילו תרגום מספר למחרוזת (או מחרוזת למחרוזת) מכונה format אבל לשם הפשטות התייחסתי לשני הסוגים בשם format במסגרת דיון זה.
חגי
... View more