BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
배서우
Calcite | Level 5

안녕하세요, SAS 코드가 도무지 떠오르지 않아 질문올립니다!

 

ID별로 각 Index_Date로부터 90일 이내에 해당하는 각 행의 dose들을 더하여 dose_90변수를 만들고자합니다. 즉, 예를들어 아래 그림처럼 Index_Date 가 2001년 9월 27일일 때, 해당 ID의 다른 index_date중 2001년 9월 27일로부터 90일 이내에 해당하는 index_date(즉, 예시에선 2001년 10월 3일) 의 dose(20mg)를 기존 행의 10mg과 더하여 30mg이 산출되게끔 하고싶습니다.

_0-1628492454587.png

이 경우 sas 코드를 어떻게 짜야할까요? (참고로 그림엔 보이지 않지만, Index_date는 input(INDEX_DATE, yymmdd8.)을 통해 숫자로 변환하여 날짜차이 계산이 용이하게 해놓았습니다.)

 

도움 부탁드려요!! 감사합니다.

 

1 ACCEPTED SOLUTION

Accepted Solutions
AmeeKang
Quartz | Level 8

 

proc sql문에 조건문과 inner join을 하시면 됩니다.

 

data a;
	input id date1 :yymmdd10. dose;
	format date1 yymmdd10. date2 yymmdd10.;
	date2 = date1 + 90;
cards;
1 20010927 10
1 20011003 20
1 20020204 100
2 20010314 10
2 20010426 50
2 20010509 25
2 20010603 10
;
run;

proc sql;
create table a as
  select a.id, a.date1, a.dose
       , sum(b.dose) as total
  from have a 
    inner join have b
    on a.id = b.id 
      and b.date1 between a.date1 and a.date1+90
  group by a.id,a.date1,a.dose
  order by id,date1
;
quit;

proc print;
run;

[결과]

스크린샷 2021-08-15 오전 9.03.42.png

View solution in original post

1 REPLY 1
AmeeKang
Quartz | Level 8

 

proc sql문에 조건문과 inner join을 하시면 됩니다.

 

data a;
	input id date1 :yymmdd10. dose;
	format date1 yymmdd10. date2 yymmdd10.;
	date2 = date1 + 90;
cards;
1 20010927 10
1 20011003 20
1 20020204 100
2 20010314 10
2 20010426 50
2 20010509 25
2 20010603 10
;
run;

proc sql;
create table a as
  select a.id, a.date1, a.dose
       , sum(b.dose) as total
  from have a 
    inner join have b
    on a.id = b.id 
      and b.date1 between a.date1 and a.date1+90
  group by a.id,a.date1,a.dose
  order by id,date1
;
quit;

proc print;
run;

[결과]

스크린샷 2021-08-15 오전 9.03.42.png