Given the SAS data set WORK.ORDERS:
order_id |
customer |
shipped |
9341 |
Josh Martin |
02FEB2009 |
9874 |
Rachel Lords |
14MAR2009 |
10233 |
Takashi Sato |
07JUL2009 |
The variable order_id is numeric; customer is character; and shipped is numeric, contains a SAS date value,and is shown with the DATE9. format.
A programmer would like to create a new variable, ship_note,that shows a character value with the order_id,shipped date, and customer name.
For example, given the first observation ship_note would have the value “Order 9341 shipped on 02FEB2009 to Josh Martin”.
Which of the following statement will correctly create the value and assign it to ship_note?
A. ship_note=catx(‘ ‘,’Order’,order_id,’shipped on’,input(shipped,date9.),’to’,customer);
B. ship_note=catx(‘ ‘,’Order’,order_id,’shipped on’,char(shipped,date9.),’to’,customer);
C. ship_note=catx(‘ ‘,’Order’,order_id,’shipped on’,tranwrd(shipped,date9.),’to’,customer);
D. ship_note=catx(‘ ‘,’Order’,order_id,’shipped on’,put(shipped,date9.),’to’,customer);
Check AnswerHide AnswerAnswer: D
注解:CHAR function的作用是返回字符串中某一字符的位置,比如char(‘SAS’, 3),返回的值为’S’,char(‘SAS’, 4)则返回missing。
TRANWRD function用于替换字符串中的某些字符,比如tranwrd(‘SAS&R’, ‘R’, ‘Python’),返回的值为’SAS&Python’。
PUT function的作用是将某个值用特定的格式输出,用法为:
new_variable = PUT(source_value, format);
即format指定的是new_variable的格式。比如题目中,将一个SAS date(距1960年1月1日的天数),用date9.0的格式表示。
INPUT function则正好与PUT相反,用于指定读取某个值的格式,用法为:
new_variable = INPUT(source_value, informat);
即informat用于告诉SAS source_value的格式。
希望以下代码会有助于对PUT和INPUT的理解:
data d;
today1 = ’18Nov2014’d;
new_today1 = put(today1, ddmmyy10.);
today2 = ’18/11/2014′;
new_today2 = input(today2, ddmmyy10.);
run;
Obs |
today1 |
new_today1 |
today2 |
new_today2 |
1 |
20045 |
18/11/2014 |
18/11/2014 |
20045 |