본문 바로가기

자격증/SAS BASE

SAS Crambible 정리

오답, 헷갈렸던 것 위주 정리

 

Q4. 데이터 읽기 - output

 

Given the SAS data set SASDATA.TWO:

X Y

5 2

3 1

5 6

 

The following SAS program is submitted: (문제 수정)

 

data sasuser.ONE sasuser.TWO other;

set sasdata.two;

  if X = 5 then output sasuser.ONE;

  if Y < 5  then output sasuser.TWO;

output;

run;

 

 

Q. What is the result?

data set SASUSER.ONE has 5 observation.

data set SASUSER.TWO has 5 observation.

data set WORK.OTHER has 3 observation.

 

ONE TWO OTHER
5 2 5 2 5 2
5 2 5 2  
3 1 3 1 3 1
   3 1  
5 6 5 6 5 6
5 6    

 

  • 첫 번째 데이터 불러왔을 때 5 2 5 2 5 2
  • 첫번째 IF → X= 5  와 Y < 5 를 만족하므로 SASUSER.ONE, SASUSER.TWO 에 들어간다.
  • SASUSER.ONE,SASUSER.TWO 에는 5개의 관측치, WORK.OTHER 에는 3개의 관측치가 들어간다.

 

 

 

 

Q9. Merge A B (rename=(바꿀 변수=바뀔변수))

The SAS data sets WORK.EMPLOYEE and WORK.SALARY are shown below:

 

WORK.EMPLOYEE WORK.SALARY

fname age  name salary  Bruce 30 Bruce 25000

Dan 40  Bruce 35000

Dan 25000

 

The following SAS program is submitted:

 

data work.empdata;

by fname;

totsal + salary;

run;

 

Q. Which one of the following statements completes the merge of the two datasets by the FNAME variable?

 merge work.employee work.salary(rename=(name=fname));

 

  • work.employee 에는 fname,age 변수 존재
  • work.salary 에는 name,salary 변수 존재
  • work.salary 의 name 변수를 fname 으로 바꿔주어야 한다.

 

 

 

 

Q18. PROC PRINT 문에서 SUM statement

 

The SAS data set PETS is sorted by the variables TYPE and BREED.

 

The following SAS program is submitted:

 

proc print data=pets;

  var type breed;

  sum number;

run;

 

 

  • The SUM statement produced only a grand total of NUMBER
  • SUM은 총합을 계산해주므로, grand total 만 들어가게 된다.

 

(코드로 확인)

 

 

 

 

 

Q21. DO 반복문에서 output 의 유무에 따른 결과 차이

 

[OUTPUT 이 있는 경우]  Q. WORK.sales 셋에 쓰여지는 관측값 수 : 60

 

data work.sales;

  do year = 1 to 5;

  do month = 1 to 12;

  x + 1;

  output;

  end;

  end;

run;

 

  • OUTPUT 은 계산 결과를 저장한다.

 

[OUTPUT 이 있는 경우]   Q. WORK.sales 셋에 쓰여지는 관측값 수 : 1

 

data work.sales;

  do year = 1 to 5;

  do month = 1 to 12;

  x + 1;

  end;

  end;

run;

 

  • 따로 output 으로 보내는 설정이 없어서 계속 반복문이 덮어쓰이게 된다.  ★
  • 최종적으로 PDV 에는 

        year  month  X

         5       12     60   

  이렇게 저장되며 observation은 결국 1개가 된다.

 

 

 

 

Q29. VARNUM 옵션

The following SAS program is submitted:

 

PROC CONTENTS data=sashelp.class varnum;

quit;

 

Q. What does the VARNUM option print?

  • a list of the variables in the order they were created.

 

  • Varnum : 변수를 작성순으로 표시 (따로 표시 안해주면 디폴트 값으로 알파벳순으로 정렬됨)
  • varnum 옵션이 들어가있으면, 조회하는 테이블에서 테이블이 포함된 변수들이 만들어진 순서를 보여준다.

 

 

 

 

Q35. PROC FREQ 옵션들

The following SAS program s submitted:

 

proc freq data=class;

tables gender * age / <insert option here>;

run;

 

The following report is created:

~~~    → report 결과를 보고 option 을 맞추는 문제  (정답 : CROSSLIST)

 

 

기본

proc freq data=class; 

tables gender * age;

run;

  • 기본적으로 빈도, 백분율, 행백분율, 컬럼백분율이 생성된다.

list

proc freq data=class;

tables gender * age / list;

run;

  • list 를 적용하게 되면 list 형태로 빈도, 백분율, 누적빈도, 누적 백분율이 생성된다.

ncol

proc freq data=class;

tables gender * age / nocol;

run;

  • 빈도, 백분율, 행백분율 (컬럼백분율이 빠진다.)

crosslist

proc freq data=class;

tables gender * age / crosslist;

run;

  • 리스트 형태로 보여주지만 단순 list 에서, 각 카테고리별로, 그리고 그 최종합계도 생성된다.

 

 

 

 

40. MISSOVER

Q. What is the purpose or the MISSOVER option on the INFILE statement?

  • It prevents SAS from loading a new record when the end of the current record is reached.

 

  • 현재 record가 끝난 다음에 new record를 로딩하도록 해주는 옵션
  • Missing value 가 있는 데이터를 불러올 때 MISSOVER 옵션이 없으면,
  • missing value값이 제대로 표현되지 않고 데이터가 밀려서 들어오게 된다.
  • 이처럼 missing value 가 있는 데이터를 불러올 때 MISSOVER 옵션을 사용해야 한다.

 

 

 

 

Q47. 사용자 정의 format

Q. A user-defined format has been created using the FORMAT procedure. Where is it stored?

  • In a  SAS catalog.Q

 

 

 

 

Q52. DATA _null_

 

The following SAS program is submitted:

 

DATA_null_;

set old;

put sales1 sales2;

run;

 

 

Q. Where is the output written?  (To the SAS log)

  • old 데이터를 이용하여 진행하고 싶은데 데이터셋으로 지정하고 싶지 않을 때
  • 결과물을 output window 에서 보는 것이 아니라, log 창으로 결과물을 보고싶을 때 사용함.

 

 

 

 

Q53. _temporary_

 

data work.test;

array items{3}_temporary_;  -------------→  item 이라는 이름을 가진 길이 3의 array 를 만드려고 한다.

run;

 

Q. What are the names of the variable(s) in the WORK.Test data set?  

  • No variables are created because it is a temporary array.

 

  • Item 이라는 이름을 가진 길이 3의 array 를 만드려고 한다. 
  • _temporary_ 를 이용하여 임시적으로 할당함
  • 즉, 데이터에 출력이 안됨 !!  (중간 계산 용도로 사용한다 !!)

 

 

 

 

Q55. format 변수 weekdate

 

The following SAS program is submitted:

 

data one;

date ='04jul2005'd;

format date weekdate;

run;

 

 

Q. What output is generated?   (Obs date 1 Monday, July 4, 2005)       

 

  • Weekday defualt 값 : Monday, July 4, 2005
  • weekday3. : Mon
  • weekday6. : Monday
  • weekday17. : Monday,July4,2005
  • weekday21. : Monday, July 4, 2005

 

 

 

 

Q57. 인수없이 사용되는 PUT 구문 (NUll PUT)

Given the SAS data set PERM.STUDENTS:

PERM.STUDENTS NAME AGE -------- -----

Alfred 14

Alice 13

Barbara 13

Carol 14

 

 

The following SAS program is submitted:

libname perm 'SAS data library';

data students;

set perm.students;

file 'file specifivation';

put name $ age;

< insert statement here >

run;

 

The following double-spaced file is desired as output.

Alfred  14       -------------→ 결과물을 double-spaced 파일로 지정하고 싶음. 

Alice  13                                       (데이터 넣고 한칸 띄움 → 다시 데이터 넣음 반복...)

Barbara  13

Carol  14

 

 

 

Q. Which statement completes the program and creates the desired file?   (PUT)

  1. put; : 한 칸씩 행을 벌려준다.
  2. put/; : 두 칸씩 행을 벌려준다. double-spaced 가 아니라 triple-spaced 가 된다.
  3. double ; : 에러 발생
  4. PUT _NULL_; : double-spaced 이긴 하지만 벌린 행에 .(period) 를 넣어준다.

 

 

(코드로 확인)

 

 

PUT 구문을 사용하지 않은 코드

 

 

 

PUT : 한 칸씩 행을 벌려준다.

 

 

PUT/ : 두 칸씩 행을 벌려준다.

 

 

PUT _NULL_ : 벌린 행에 period (.) 를 넣는다.

 

 

 

 

Q59. 날짜 Format

Given the contents of the raw data file EMPLOYEE:

----|----10----|----20----|----30

Alan          19/2/2004 ACCT

Rob          22/5/2004 MKTG

MaryJane          14/3/2004 EDUC

 

The following SAS program is submitted:

 

data emps;

infile 'employee';

input @1 name$ @15 date <insert INFORMAT here> @25 department$;

run;

 

 

Q. Which INFORMAT correctly completes the program?  (ddmmyy10.)

  1. date9.    러 (X)
  2. ddmmyyyy9.    러 (X)
  3. ddmmyy10.   (O)
  4. ddmmyyyy10.    (X)

 

  • @숫자 :  '숫자' 지점부터 변수에 대한 값을 불러오겠다는 명령어
  • input 을 이용하여 name, date, department 변수를 만들고 있음
  • @1 부터 쭉 불러서 name 에 넣고, @15부터 date로, @25 부터 department 로 넣음.
  • 이 때 Informat을 사용 !!  (데이터를 불러올 시, format 을 바꿀 때 informat 을 사용한다.)

 

출처 :&nbsp; https://wikidocs.net/31042

 

 

 

 

Q60. @@

  • Two @@ hold the raw data record across iterations of the DATA step.

 

 

 

 

Q63.  First.변수 (in Sorted data set)

 

Given the SAS data set ONE:

 

X Y Z

1 A 27

1 A 33

1 B 45

2 A 52

2 B 69

3 B 70

4 A 82

4 C 91

 

The following SAS program is submitted:

 

data work.two;

 set work.one;

 by X Y;

 if first.Y;

run;

 

proc print data=work.two;

run;

 

 

Q. Which report is produced? 

X Y Z First.Y
1 A 27 1
1 A 33 0
1 B 45 1
2 A 52 1
2 B 69 1
3 B 70 1
4 A 82 1
4 C 91 1
  • 정렬된 X 변수 내에서 Y 변수 기준으로 First 값을 계산해야 한다.

 

X Y Z
1 A 27
1 B 45
2 A 52
2 B 69
3 B 70
4 A 82
4 C  91

 

 

 

 

Q67. 문자형, 숫자형 전환 error

Given the raw data file AMOUNT:

 

----|----10----|----20----|----30

$1,234    -------------→ $  , 존재하므로 문자형 변수이다.

 

The following SAS program is submitted:

 

data test;

infile 'amount';

input@1 salary 6.;  -------------→ 첫번째부터 불러와서 6자리 salary 변수를 만들고자 함.

if_error_then description = "Problems';

else description = "No Problems";

run;

 

 

Q. What is the result?    (The value of the DESCRIPTION variable is Problems.)

 

  • salary 변수에 $ 표시가 없는 걸 보니 수치형 변수로 선언했는데  
  • amount 값을 보면 $(달러) 도 있고, 도 있어서 문자형 변수이다.
  • 문자형 변수를 숫자형 변수로 바꾸려고 하니 에러가 생긴다 !! 

 

 

 

 

Q68.  @5 age 2.;

 

Given the SAS data set PEPM.STUDENTS : PERM.STUDENTS :

 

NAME  AGE   -------------→  PUT statement

---------  -----

Alfred 14

Alice 13

Barbara 13

Carol 14

 

The following SAS program is submitted:

 

libname perm 'SAS data library';

data student;

set perm.student;

file 'file specification';

put name $15. @5 age 2.;  -------------→ name : 문자형 변수($), 15자리 할당, age : 5번째 자리부터 2자리까지 읽어오기

run;

 

 

 

 

Q. What is written to the output raw data file?   (B)

 

  • name 변수를 위해 문자형 변수로 15자리까지 할당해주었으나, 뒤의 @5 때문에 age 를 덮어버리게 된다.
  • 따라서 Alfr14 와 같은 형태가 나오게 된다.
  • name 이라는 변수 아래 Alfred 를 넣고,  name 은 15자리 까지 넣을 수 있는 자리확보가 되어있다.
  • age 라는 변수에는 @5, 즉 5번째 자리부터 age 값을 넣으라고 하므로
  • 14라는 age 값이 강제로 5번째 자리부터 들어가게 되어서 결국 Alfr14 가 된다.

 

(코드로 확인)

 

 

- INPUT statement 와 PUT statement 의 차이

  • The INPUT statement reads data
  • while The PUT statement writes data values, text strings, or both to the SAS log or to an external file.

 

 

 

 

Q73. Infile 문에서 truncover

 

Given the contents of the raw data file TYPECOLOR.DAT:

----+----10----+----20----+----30

daisyyellow    -------------→ 11자리

 

The following SAS program is submitted:

 

data FLOWERS;

infile 'TYPECOLOR.DAT' truncover -------------→ truncover 라는 옵션이 걸려있음

length Type $ 5 Color $ 11;

input Type $ Color $; run;

 

 

Q. What are the values of the variables Type and Color?      (Type=daisy, Color=)

 

  • 넣으려고 하는 값의 길이는 11, 넣어주려고 하는 변수 type 의 길이는 5 이다.
  • 따라서 truncover 옵션이 없으면, color 에서 에러가 발생하게 된다.

 

  • truncover : 읽을 데이터가 없어 에러가 발생하는 것을 방지한다.
  • type 의 길이가 5로 지정되어 있으므로 daisy 까지 들어가게 된다.
  • (읽을 수 있는 만큼만 읽어오고 나머지는 버림)
  • 따라서 type 에는 daisy만 들어있고, 그 다음 불러올 데이터가 없지만 에러는 뜨지 않고 missing value 라고만 들어가 있도록 color에는 아무런 값이 들어가지 않게 된다.

 

 

 

 

86. %Manager%

Given the data set WORK.EMPDATA:

 

 

Q. Which one of the following where statements would display observations with job titles containing the word 'Manager'?       (Where Job_Title like '%Manager%';)

 

 

  • % : 와일드카드, 어떤 문자의 앞 혹은 뒤에 어떤 문자가 와도 괜찮다는 의미.  갯수 제한이 없다.
  • '%Manager ' : Manager 앞에 아무거나 와도 상관이없음. 대신 뒤에 공백이 있어야 함.

 

 

 

 

 

Q87 "01Jan1906'(공백)D

 

The following SAS program is submitted:

 

data WORK.DATE_INFO;

X="01JAN1960" D   -------------→ D 앞뒤로 공백이 있다

run;

 

Q. What variable X contains what value?  (the code contains a syntax error and does not execute.)

 

  1. the numeric value 0    (X)
  2. the character value "01Jan1960"    (X)
  3. the data value 01011960    (X)
  4. the code contains a syntax error and does not execute.    (O)

 

  • D의 앞뒤로 공백이 없어야 한다.
  • 만약 공백이 없으면 값이 0 이다. (1960년 1월 1일 이 0 이기 때문.)
  • D 의 의미 : 1960.01.01 기준으로 며칠의 차이가 있는 건지 숫자로 결과값을 나타내라는 의미  
  • 만약 "02Jan1960"D 라면 1960.1.1 이 0 이기 때문에, 차이가 1이므로 X 값이 1 이된다.  

 

 

 

 

Q89. proc contents data=_all_;

The following program is submitted:

 

proc contetns data=_all_;

run;

 

Q. Which statement best describes the output from the submitted program?

  • The output contains a list of the SAS data sets are contained in the WORK library and displays the contents of thoese data sets.  

 

  • 보통 라이브러리이름.데이터셋 이름 으로 표현을 하는데,
  • 라이브러리 이름이 없으므로 WORK 라이브러리 대상으로 그 라이브러리 안에있는 모든 것들에 대한 정보를 준다.

 

 

 

 

Q102. execution error

you're attempting to read a raw data file and tou see the follwing messages displayed in the SAS Log:

 

NOTE : Invalid data for Salary in line 4 15-23.

 

RULE: ----+----1----+----2----+----3----+----4----+----5---

4 120104 F 46#30 1MAY1954 33

 

Employee_Id=120104 employee_gender=F Salary=. birth_data=-2061 _ERROR_=1 _N_=4  -----------→  PDV 

 

NOTE: 20 records were read from the infile 'c:/employees.dat'.

 

The minimum record length was 33.

The maximum record length was 33.

 

 

NOTE: The data set WORK.EMPLOYEES has 20 observations and 4 variables.

 

 

Q. What does it mean?

  1. A compiler error, triggered by an invalid character for the variable Salary.
  2. An execution error, triggered by an invalid character for the variable Salary.   (O)
  3. The 1st of potentially many error, this one occurring on the 4th observation.
  4. An error on the INPUT statement specification for reading the variable Salary.

 

 

 

 

 

Q141. Do 반복문

The SAS data set BANKS is listed below:

 

 

WORK.network

 

Q. how many observations and variables will exist in the SAS data set NETWORK?

  • 1 observation and 2 variable

'자격증 > SAS BASE' 카테고리의 다른 글

Sample Questions (Project)  (0) 2023.01.20
SAS Certification Base Programming 연습문제  (0) 2023.01.18
SAS Crambible 103 ~ 139.  (1) 2023.01.15
Chapter 5. Reading SAS Data Sets  (0) 2023.01.06
Chapter 4. Formatting Data Values  (0) 2023.01.04