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

I have a question about date conversions. I have a proc sql code where I need to take a the dates of the year and assign a number to it

I can do a case statement and say

case fst_srvc_dt = '1/1/2011' then 0 end as date

case fst_srvc-dt = '1/2/2011' then 1 end as date1

and so on. What I need is each date to equal a day so when I get to 12/31/2011 it is 365. I know my case statement is not 100% right and know how to build them just doing this quick and dirty type thing. There has to be an easier way to do this. Maybe something that says if my date is between 1/1/2011 and 12/31/2011 start with 1/1/2011 =0 and add +1 as it goes to 12/31/2011.

This is needed because we have 100's of patients with various dates of service in this time period and we want the patient ot be assigned a number for the date of service rather than the actual date. So I might end up with 50 patients that had a date of service that was 3/31/2011, then all those 50 patients would be assigned 90 as the number.

THanks

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Like Paige said, the intck function will work, as will the datdif function.  The latter might be easier if you want to alter the day count based on standardizing the number of days to, say, 360, 365 or the actual number of days in the year.  e.g.:

data have;

  format fst_srvc_dt date9.;

  do fst_srvc_dt=18628 to 18628+364;

    id+1;

    output;

  end;

run;

proc sql;

  create table want as

    select id, datdif(mdy(1,1,year(fst_srvc_dt)), fst_srvc_dt, 'act/act') as date

      from have

  ;

quit;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

The INTCK function does exactly what you need

--
Paige Miller
art297
Opal | Level 21

Like Paige said, the intck function will work, as will the datdif function.  The latter might be easier if you want to alter the day count based on standardizing the number of days to, say, 360, 365 or the actual number of days in the year.  e.g.:

data have;

  format fst_srvc_dt date9.;

  do fst_srvc_dt=18628 to 18628+364;

    id+1;

    output;

  end;

run;

proc sql;

  create table want as

    select id, datdif(mdy(1,1,year(fst_srvc_dt)), fst_srvc_dt, 'act/act') as date

      from have

  ;

quit;

tish
Calcite | Level 5

I don't think you need a case statement. I'd read the character field fst_srvc_dt in as a SAS date, convert it to a Julian date, extract the day segment and subtract 1:

put(substr(put(input(fst_srvc_dt, anydtdte10.), julian5.), 3), 3.) - 1 as date

Note that the highest value will be 364 for 2011 and that you will get just one date variable. If you want 365 of them, I'd use PROC TRANSPOSE to transpose the data.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 949 views
  • 0 likes
  • 4 in conversation