BookmarkSubscribeRSS Feed
mandan414
Fluorite | Level 6

Hi everybody,

 

I have a dataset of students in a high school. The data includes the start-date and end-date for each student. I would like to make an snapshot of a date, e.g. 31 december of each year, and find out if the respondent attended in the high school on that date. 

 

student        start_date              end_date

1                 04 Sept, 1993         26 june, 1995

2                 22 Nov, 2002          20 May, 2005

......

I would like to make a variable which is if the student 1 attended the high school at 31 december of each year between 1993 to 1995, then the value of the new variable is 1 otherwise 0, and the same for each pther student. 

Thank you!

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@mandan414 wrote:

 

student        start_date              end_date

1                 04 Sept, 1993         26 june, 1995

2                 22 Nov, 2002          20 May, 2005


Are these true numeric variables that are SAS date values, formatted? Or are they text strings? 

--
Paige Miller
mandan414
Fluorite | Level 6

They are the true values of the SAS, numeric, date ......

PaigeMiller
Diamond | Level 26

@mandan414 wrote:

They are the true values of the SAS, numeric, date ......


In your DATA step use

 

flag1993 = (start_date<='31DEC1993'd<=end_date);
flag1994 = (start_date<='31DEC1994'd<=end_date);
/* etc. */

 

 

--
Paige Miller
mandan414
Fluorite | Level 6

That is great, but it will be very long since the dataset covers almost 50 years. I think I need to go with Do statement, but I'm not good at that. Thank you!

PaigeMiller
Diamond | Level 26

If you mean that you want to take the two lines I showed for 1993 and 1994, and you want similar for all years (for example) 1950 to 2000, then this is how you do it.

 

data want;
    set have;
    array flag flag1950-flag2000;
    year=1950;
    do i=1 to dim(yr);
        flag(i)=(start_date<=mdy(12,31,year)<=end_date);
        year=year+1;
    end;
    drop i year;
run;
         

 

--
Paige Miller
ballardw
Super User

Another way creating one record per "flagged year";

data have;
  input student  start_date :date9. end_date :date9.;
  format start_date end_date date9.;
datalines;
1  04Sep1993 26jun1995
2  22Nov2002 20May2005
;

data want;
   set have;
   do d = start_date to end_date;
         d= intnx('year',d,0,'e');
         flagyear = year(d);
         if d le end_date then output;
         d=d+1;
   end;
   drop d;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 718 views
  • 0 likes
  • 3 in conversation