나는 SQL에서 if / then 문을 구문 (예를 들어 사례를 사용하든) 및 / 또는 동일한 쿼리에서 변수 서식을 지정하는 방법에 대한 고급 SQL 지식이 없으므로 누군가이 sas 코드를 도울 수 있는지 궁금합니다. sql에 올바른 형식으로 구문을 지정합니다.
data convert_code;
format date1 date 2 mmddyy8. code $4.;
set userid.code; (this table is pulled from oracle)
if ID='X' then P='A'; else P='B';
If CAT in ('1','2','3') then CAT_group='ONE'; else CAT_GROUP='TWO';
if CAT_2 > '0' and CAT_2A >='1' then d=1; else d=0;
date1=datepart(date1);
date2=datepart(date2);
if code='3' and type_code in ('A','B','C') THEN DO;
if P_CODE in ('1','2','3') then P='1';
if P_CODE in ('4','5','6') then P='2';
end;
if code='4' and e_code in ('A') then DO;
if B_CODE in ('11','12','13') then P='3';
if B_CODE in ('14','15','16') then P='4';
end;
run;
여기에 제공된 예는 SAS의 proc sql
언어를 사용 합니다. 다른 SQL 구현을 사용하는 경우 구문이 다를 수 있습니다. 그러나 case
표현식 예제는 SQL 표준의 일부이므로 모든 SQL 구현에 적용하기가 매우 쉬워야합니다.
proc sql;
/* Name your output table */
create table convert_code as
select
/*
Unlike the data step variables from the input data are not
selected by default, you can request them individually or
with "select *"
*/
/* Use "format =" after the column definition to set its format */
code format = $4.,
/* Use SAS functions as normal, name the output variable with "as" */
datepart(date1) as date1 format = mmddyy8.,
/* Comma separate each variable you want in your output */
datepart(date2) as date2 format = mmddyy8.,
/* A "case" expression can conditionally set a variable to a value */
case
when CAT in ('1', '2', '3') then 'ONE'
else 'TWO'
/* Close the "case" statement with "end" */
end as CAT_group,
/* You can nest case statements to emulate your "then do" */
case
when code = '3' and e_code in ('A', 'B', 'C') then
case
/* Use multiple "when then"s to emulate "else if" */
when P_CODE in ('1', '2', '3') then '1'
when P_CODE in ('4', '5', '6') then '2'
else ''
end
when code = '4' and e_code in ('A') then
case
when P_CODE in ('11', '12', '13') then '3'
when P_CODE in ('14', '15', '16') then '4'
else ''
end
when ID = 'X' then 'A'
else 'B'
end as P,
/* An alternative to case is to use the "ifn()" or "ifc()" functions */
ifn(CAT_2 > 0 and CAT_2A >= 1, 1, 2) as d
/* Choose the input data and end the query with a ";" */
from userid.code;
/*
Additional processing can be done here, some examples include:
"where": limit the input
"left join", "right join", "inner join", "outer join", ",":
combine with additional data sets
"group by": group based on column values for summary functions
"order by": specify which columns to sort the output by
*/
/* End the "proc sql" processing */
quit;
조건부로 값을 할당하는 표준 SQL 방법이고 다른 SQL 구현에서도 동일 할 것이므로 case
SAS 특정 ifn()
및 ifc()
함수 보다는 표현식을 사용 하는 것이 제 제안입니다. 그주의 format =
와 datepart()
SAS의 고유 한 기능.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다