Q. Within the data set Cert.Temp, PayRate is a character variable and Hours is a numeric variable. What happens when the following program is run?
data work.temp ;
set cert. temp;
Salary = payrate * hours ;
run;
- SAS converts the values of PayRate to numeric values. A message is written to the log.
- When this DATA step is executed, SAS automatically converts the character values of PayRate to numeric values, so that the calculation can occur.
- Whenever data is automatically converted, a message is written to SAS log stating that the conversion has occured.
Q. A typical value for the chracter variable Target is 123,456. Which statement corretly converts the values of Target to numeric values when creating the variable TargetNo?
- ★ Character → Numeric -
- TargetNo = input(target,comma7.) ;
- You explicitly convert character values to numeric values by using INPUT function.
- Be sure to select an informat that can read the form of the values.
- INPUT function, Informat
Q. A typical value for the numeric variable SiteNum is 12.3. Which statement correctly converts the values of SiteNum to character values when creating the variable Location?
★ Numeric → Character -
- Location = dept||'/'||put(sitenum,4.1) ;
- || : 두 값을 이어주는 함수
- You explicitly convert numeric values to character values by using the PUT function.
- Be sure to select a format that can read the form of the values.
- PUT function, format
Q. The variable Address2 contains values such as Piscataway, NJ. How do you assign the two-letter state avbbreviations to a new variable named State?
- SCAN 함수 -
- State = scan(address2, 2) ;
- The SCAN funtion is used to extract words from a character value when you know the order of the word.
- When their position varies, and when the words are marked by some delimetier.
- In this case, you do not need to specify delimiters, because the blank and the comma are default delimiters.
- SCAN 함수 단어를 추출할 때 사용한다. 공백과 콤마는 기본 delimiters 임 !!
[ 틀린 보기 ]
- State = scan(address2,13,2) ;
- State = substr(address2,2) ;
- State = substr(address2,13,2) ;
Q. The variable IDCode contains values such as 123FA and 321MB. The fourth character identifies sex. How do you assign these character codes to a new variable named Sex?
- SUBSTR 함수 : 오른쪽에 사용된 경우 -
- Sex = substr(idcode,4,1);
- The SUBSTR function is best used when you know the exact position of the substring to extract from the character value.
- You specift the position to start from and the number of characters to extract.
- SUBSTR 함수는 추출할 문자의 정확한 위치를 알고있을 때 사용하기 좋음 !
- When placed on the right side, the SUBSTR function extracts a substring.
Q. Because of the growth within the 919 area code, the telephone exchage 555 is being reassigned to the 920 area code. The data set Clients.Piedmont includes the variable Phone, which contains telephone numbers in the form 919-555-1234. Which of the following programs corretly changes the values of Phone?
- SUBSTR 함수 : 왼쪽에 사용된 경우 -
data work.piedmont (drop=areacode exchange);
set cert.piedmont;
Areacode = substr(Phone,1,3);
Exchange=substr(Phone,5,3);
if areacode = '919' and Exchage = '555'
then substr(Phone,1,3) = '920'; ----------------→ replaced on the left side of an assignment statement
run;
- The SUBSTR function replaces variable values if it is replaced on the left side of an assignment statement.
- When placed on the right side(이전 예시), the function extracts a substring.
[ 틀린 보기 ]
data work.piedmont (drop=areacode exchange);
set cert.piedmont;
Areacode = substr(Phone,1,3);
Exchange=substr(Phone,5,3);
if areacode = '919' and Exchage = '555'
then scan(phone,1,3) =' 920'
run;
Q. Suppose you need to create the variable FullName by concatenating the values of FirstName, which contains first names, and LastName, which contain last names. What is the best way to remove extra blanks between first name and last names?
- TRIM 함수 이용하여 공백 제거하기 -
data work.maillist ;
set cert.maillist ;
length FullName $ 40 ;
fullname = trim(firstname)||' '||lastname ;
run ;
- The TRIM function removes trailing blanks from character values.
- In this case, extra blanks must be remove from the values of FirstName.
- Although answer c also works, the extra TRIM function for the variable LastName is unnneccessary.
- Because of the LENGTH statement, all values of FullName are padded to 40 chraracters.
- TRIM 함수는 후행공백을 제거
- LENGTH 문에서 FullName 의 모든 값들을 40 chrarcters 으로 지정했기 때문에 LastName 에 대해서는 TRIM 함수 적용 안함.
[ 틀린 보기 ]
a.
data work.maillist ;
set cert.maillist ;
length FullName $ 40;
fullname = trim firstname||' '||lastname ;
run ;
b.
data work.maillist ;
set cert.maillist ;
length FullName $ 40;
fullname = trim(firstname||' '||lastname) ;
run ;
c.
data work.maillist ;
set cert.maillist ;
length FullName $ 40;
fullname = trim(firstname)||' '||trim(lastname) ;
run ;
Q. Within the data set Cert.Bookcase, the variable Finish contains values such as ash,cherry, teak, matte-black. Which of the following creates a subset of the data in which the values of Finish contain the string walnut? Make the search for the string case-insensitive.
- INDEX 함수 -
data work.bookcase ;
set cert.bookcase ;
if index(lowcase(finish) , 'walnut') > 0 ;
run ;
- case-insensitive : 대.소문자의 구분없이
- Use INDEX function in a substring IF statement, enclosing the charater string in quotation marks.
- Only those observations in which the function locates the string and returns a value greater than 0 are written to the data set.
- 함수가 문자열을 찾고 0보다 큰 값을 반환하는 관측치만 데이터 집합에 기록한다.
'자격증 > SAS BASE' 카테고리의 다른 글
| SAS 관련 정리 (2) | 2022.12.25 |
|---|---|
| Chapter 16. Creating Output (0) | 2022.12.22 |
| Chapter 13. SAS Date, Time, ane Datetime Values. (0) | 2022.12.19 |
| Chapter 12. SAS Formats and informats (0) | 2022.12.19 |
| Chapter 11 . Processing Data with Do Loops (0) | 2022.12.17 |