שבוע שעבר דיברנו על שני סוגי המשתנים שיש ב – SAS, מחרוזת ומספר ועל שתי הפונקציות שמאפשרות לנו להמיר מסוג אחד לשני, PUT ו – INPUT. החלק היותר מעניין בשתי הפונקציות הללו הוא הפרמטר השני שלהן שאומר ל - SAS איך אנחנו רוצים לבצע את ההמרה.
יש הרבה מהמשותף בין format ל – informat במבנה. השם שלהם תמיד מורכב מצירוף של אותיות כלשהו עם נקודה בסוף כאשר הנקודה היא חלק בלתי נפרד מהשם. הנקודה הזו גם מאפשרת לנו לבצע קצת התאמה אישית של כל format ו – informat כפי שנראה בהמשך.
נתחיל בהמרה ממספר למחרוזת בעזרת הפונקציה PUT. הפרמטר השני שהפונקציה הזו מקבלת נקרא format והוא התבנית שבעזרתה אנחנו רוצים לבצע את ההמרה. כפי שצוין קודם אנחנו יכולים לבצע התאמה מדויקת יותר של כל format לצרכינו ע"י הוספת מספר לפני ומספר אחרי הנקודה בשם של ה – format. אם נוסיף מספר בין שם הפורמט לבין הנקודה אז הוא יקבע את סה"כ התווים במחרוזת שנקבל כולל כל התווים המיוחדים שה – format יוסיף עבורנו כגון נקודה עשרונית, פסיקים של אלפים וכו'. אם נוסיף מספר אחרי הנקודה הוא יציין ל – SAS כמה ספרות אחרי הנקודה אנחנו רוצים שיופיעו במחרוזת שלנו. במידה ואנחנו לא מציינים מספר לפני או אחרי הנקודה כאשר אנחנו מגדירים format אז SAS עושה שימוש בערכי ברירת המחדל של כל format המפורטים בתיעוד.
ונעבור לדוגמאות:
data NUM_TO_CHAR;
length
Mispar 8 /*This is a numeric column*/
Machrozet $100 /*This column is string column*/;
call missing(machrozet);
input mispar;
datalines;
102.54
102.54
54631372.135474
54631372.135474
1697854259
20154
148
14587
;
run;
data NUM_TO_CHAR;
set NUM_TO_CHAR;
if _n_=1 then machrozet=put(mispar, 32.); * 32 is the maximum size. Note the rounding and the spaces ahead of the digits;
if _n_=2 then machrozet=strip(put(mispar, 32.5)); * Removing these spaces and getting crazy with digits to the right of the decimal point;
if _n_=3 then machrozet=strip(put(mispar, comma20.)); * Adding some thousands separators;
if _n_=4 then machrozet=strip(put(mispar, comma5.)); * If selecting too few characters SAS does the best it can;
if _n_=5 then machrozet=strip(put(mispar, sizekmg.2)); * There are really a lot of formats;
if _n_=6 then machrozet=put(mispar, ddmmyy10.); * Dates are also numbers. This way humans can read them too;
if _n_=7 then machrozet=strip(put(mispar, roman10.)); * Really really a lot of formats;
if _n_=8 then machrozet=put(mispar, tod8.); * Time formats too;
run;
ההמרה בין מחרוזת למספר מבוצעת ע"י הפונקציה INPUT והפרמטר השני שהיא מקבלת נקרא informat. בשבוע שעבר דיברנו על כך שתאריכים ב – SAS הם, למעשה, גם מספרים ולכן שימוש נפוץ מאוד ב – informat הוא להמיר תאריכים שמגיעים כמחרוזות (למשל בקובץ CSV) לתאריך בצורה הנכונה – למשל האם הספרות הראשונות הן החודש או היום כמו שניתן לראות בין הדוגמאות הבאות:
data CHAR_TO_NUM;
length
Machrozet $100 /*This column is string column*/
Mispar 8 /*This is a numeric column*/;
call missing(mispar);
input machrozet;
datalines;
06/08/2020
08jun2020
689,832,879.45
324.123
10:45
not_good
;
run;
data CHAR_TO_NUM;
set CHAR_TO_NUM;
if _n_=1 then mispar=input(machrozet, ddmmyy10.); * We have 10 characters;
if _n_=2 then mispar=input(machrozet, date.); * 9 is the default;
if _n_=3 then mispar=input(machrozet, comma20.); * SAS reads the digits to the right of the decimal point even if we did not mention them;
if _n_=4 then mispar=input(machrozet, best20.); * Letting SAS decide what is the best informat;
if _n_=5 then mispar=input(machrozet, time5.); * Of course time too;
if _n_=6 then mispar=input(machrozet, best.); * Nothing can be done;
run;
חגי
... View more