How to format dates for use in SAS?

假如想象 提交于 2021-02-11 06:35:34

问题


I am trying to adapt Method 4 in this paper to calculate the duration of many observations, but discounting overlapping dates: https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/048-31.pdf

For example, two rows of observations for subject 101 lasting from 2017-03-02 to 2017-03-16 and 2017-03-04 to 2017-03-17 respectively should return a value of only 16 days.

I am getting an error with the dates being 'Invalid numeric data', though, resulting in later errors. I have tried format startdate yyyymmdd10.; and format stopdate yyyymmdd10.; with no success.

Can anyone help me properly format my dates for use here, or identify any further errors?

Edit: Line 80 refers to do xdate = startdate to stopdate;.

I am still unable to convert or create the date variables as numeric/date values. I have used the following code:

data sasuser.Mdm;
set sasuser.Mdm; 
do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
    put xdate= yymmdd10.; 
output;
end;
run;

To get this output:

1    data sasuser.Mdm;
2    set sasuser.Mdm;
3    do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
4        put xdate= yymmdd10.;
5    output;
6    end;
7    run;

xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-18
xdate=2017-03-19
xdate=2017-03-20
xdate=2017-03-21
xdate=2017-02-08
xdate=2017-02-09
xdate=2017-02-10
xdate=2017-02-11
xdate=2017-02-12
xdate=2017-02-13
xdate=2017-02-14
xdate=2017-02-15
xdate=2017-02-16
xdate=2017-02-17
xdate=2017-02-18
xdate=2017-02-19
xdate=2017-02-20
xdate=2017-02-21
xdate=2017-02-22
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-25
xdate=2017-02-26
xdate=2017-02-27
xdate=2017-02-28
xdate=2017-03-01
xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-02-26
xdate=2017-02-28
xdate=2017-03-13
xdate=2017-03-17
xdate=2017-03-25
xdate=2017-03-28
xdate=2017-03-23
xdate=2017-03-24
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-27
xdate=2017-03-28
xdate=2017-03-29
xdate=2017-03-30
xdate=2017-03-29
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-05
xdate=2017-04-05
xdate=2017-04-06
xdate=2017-04-06
xdate=2017-04-07
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-30
xdate=2017-04-01
xdate=2017-04-02
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-10
xdate=2017-04-11
xdate=2017-04-12
xdate=2017-04-12
xdate=2017-04-13
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-18
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-19
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-16
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-03-27
xdate=2017-04-02
xdate=2017-04-07
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-05-03
xdate=2017-05-04
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-07
xdate=2017-05-08
xdate=2017-05-09
xdate=2017-05-10
xdate=2017-05-11
xdate=2017-05-12
xdate=2017-05-13
xdate=2017-05-14
xdate=2017-05-15
xdate=2017-05-16
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the
       BY expression is missing, zero, or invalid.
SUBJID=106 KEY=106-9 OBS=9 TOTAL=12 STARTDATE=2017-04-25 STOPDATE=  CLASS=Steroid / Diuretic
xdate=20934 _ERROR_=1 _N_=52
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 52 observations read from the data set SASUSER.MDM.
WARNING: The data set SASUSER.MDM may be incomplete.  When this step was stopped there were 431
         observations and 8 variables.
WARNING: Data set SASUSER.MDM was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.38 seconds
      cpu time            0.29 seconds```

I don't understand why input doesn't appear to be working. Dates are still listed as character strings under column attributes. The do part also isn't working as intended. I'd be grateful for any further guidance.


回答1:


  1. Do not use the same name in the DATA and SET statement. Then you're always having to rebuild from the start.
  2. Convert your start and stop date to SAS dates
  3. Remove PUT
  4. Add formats to see them displayed as desired
  5. Drop old variables to avoid confusion.

Your two code steps, the data step and SQL do not appear related. Not sure why you would even need a list of dates for intervals or anything. There are much better ways to calculate an overlap. I think you're putting us through an xy problem where it would be significantly easier to show us what you're attempting to do and people would be able to provide a much better solution.

  data sasuser.Mdm2; /*1*/
   set sasuser.Mdm; 
    /*2*/
   start_date = input(startdate, yymmdd10.);
   end_date = input(stopdate, yymmdd10.);

    do xdate = start_date to stop_date;
       output; /*3*/
     end;

    /*4*/
     format start_date end_date xDate yymmdd10.;
     /*5*/
    drop startdate stopdate;

    run;

     *check;
      proc contents data=sasuser.mdm2;
      run;

EDIT: Also, if you had some sort of grouping variable to indicate that these were part of the same episode you could then just take the min/max of the dates and subtract them to get the interval duration for starters. Grouping via a data step is trivial.

  data want;
  set have;
 by id;
 
 retain episode;
 
 start_date = input(start_date, yymmdd10.);
 end_date = input(stopdate, yymmdd10.);
 prev_stop_date = lag(stopDate);

 if first.id then do;
      episode = 0;
      call missing(prev_stop_date);
 end;

 if not (start_date <=prev_stop_date <= end_date) then episode+1;

 *could add in logic to calculate dates and durations as well depending....;

 run;



回答2:


It sounds like your SAS log is complaining about this statement.

do xdate=startdate to stopdate;

Because STARTDATE and STOPDATE are character strings instead of dates.

Make sure to create your date values as dates instead of character strings.




回答3:


Tom's correct, of course, the startdate and stopdate seem to be characters.

To properly use this, do something like this (only the do loop is relevant for you, the rest is to show it working):

data _null_;
  startdate = '2017-03-02';
  stopdate  = '2017-03-16';
  do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
    put xdate= yymmdd10.; *just put to the log to see what you are getting;
  end;
run;

input will convert the text to a numeric value. Do realize you have to format that xdate as a date format if you want to be able to view it - if you're just using it as an input, though, you can leave the formatting off.



来源:https://stackoverflow.com/questions/64172034/how-to-format-dates-for-use-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!