BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
K_Wils15
Obsidian | Level 7

Hi everyone, I am stuck on this problem: 

 

Using RETAIN, FIRST./LAST. and accumulating variables, find the total number of days each patient spent in hospital – not per episode but overall per patient; also count the number of hospital episodes for each patient.

 

The number of days for each episode (observation) is found by DISCDATE_DT – ADMIDATE_DT + 1.  Then use PROC MEANS to find the maximum and the average number of total hospital days per patient, and the maximum and average number of hospital episodes per patient.

 

Here is my log with the code I have so far. I am not sure why there are errors or how to fix them:

380 data hosp_adm3;
381 set hosp_adm2;
382 by pt_id;
383 retain prior_disc sum_days count;
384
385 if first.pt.id = 1 then do;
386 count = 0;
387 sum_days = 0;
388 end;
389 los = discdate_dt - admndate_dt + 1;
390 sum_days + los;
391 *same as sum = sum(sum, los);
392 count + 1;
393 output hosp_adm3 (drop = sum_days count);
-
22
76
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

FATAL: DATA STEP compilation stopped due to syntax errors.
NOTE: The SAS System stopped processing this step because of errors.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, RC,
_DATA_, _LAST_, _NULL_.

ERROR 76-322: Syntax error, statement will be ignored.

394
395 if last.pt_id = 1 then output hosp_adm4 (keep = pt_id sum count);
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, RC,
_DATA_, _LAST_, _NULL_.

ERROR 76-322: Syntax error, statement will be ignored.

396
397 if first.pt_id = 0 then day_com = admidate_dt - prior_disc;
398 prior_disc = discodate_dt;
399 run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Essentially, the output data set options to keep or drop variables should be part of the data statement and you should specify all output data sets on the data statement. For example:

 

data hosp_adm3(drop = sum_days count)
     hosp_adm4(keep = pt_id sum count)
     ;
   set hosp_adm2;
   by pt_id;
   retain prior_disc sum_days count;

   if first.pt.id = 1 then
   do;
      count = 0;
      sum_days = 0;
   end;

   los = discdate_dt - admndate_dt + 1;
   sum_days + los;
   *same as sum = sum(sum, los);
   count + 1;
   output hosp_adm3;

   if last.pt_id = 1 then
      output hosp_adm4;

   if first.pt_id = 0 then
      day_com = admidate_dt - prior_disc;

   prior_disc = discodate_dt;
run;

 

 

Kind regards,

Amir. 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

393 output hosp_adm3 (drop = sum_days count);
-
22
76

 

You can't use data set options like DROP= in the OUTPUT statement (as far as I know). 

 

If you want to create two output data sets, then both must be mentioned in the DATA statement, and then you can do something like this:

 

data hosp_adm3(drop=sum_days count) hosp_adm4(keep=pt_id sum count);
... programming statements ...
output hosp_adm3;
if last.pt_id then output hosp_adm4;
... more programming statements if desired ...
run;

 

--
Paige Miller
Amir
PROC Star

Essentially, the output data set options to keep or drop variables should be part of the data statement and you should specify all output data sets on the data statement. For example:

 

data hosp_adm3(drop = sum_days count)
     hosp_adm4(keep = pt_id sum count)
     ;
   set hosp_adm2;
   by pt_id;
   retain prior_disc sum_days count;

   if first.pt.id = 1 then
   do;
      count = 0;
      sum_days = 0;
   end;

   los = discdate_dt - admndate_dt + 1;
   sum_days + los;
   *same as sum = sum(sum, los);
   count + 1;
   output hosp_adm3;

   if last.pt_id = 1 then
      output hosp_adm4;

   if first.pt_id = 0 then
      day_com = admidate_dt - prior_disc;

   prior_disc = discodate_dt;
run;

 

 

Kind regards,

Amir. 

K_Wils15
Obsidian | Level 7
Thanks!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2301 views
  • 0 likes
  • 4 in conversation