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

Hi....I am trying to create multiple observations from each observation IF the Completion Date is in the next Academic Year. The Academic Year is from July 1 to the following June 30. The creation of another observation would stop when the Completion Date is in the final Academic Year, for example, for StudentUID's 12065 and 13687, the students are enrolled over 3 Academic Years. Any suggestions would be greatly appreciated...Thanks

 

 

data sets2;
    length StudentUID 8 Programs $ 22 EnrollmentStatus $ 16 RegisterStatus $ 15 TermCalendarID 8 StartDate $ 10 CompletionDate $ 10 ;
    input StudentUID : BEST32. Programs : $CHAR22. EnrollmentStatus : $CHAR16. RegisterStatus : $CHAR15. TermCalendarID : BEST32. StartDate : $CHAR10. CompletionDate : $CHAR10. ;
datalines;
785 CulinaryArts PostSecondary PostSec 108 2015-09-10 2016-06-17
2774 Autobody PostSecondary PostSec 108 2015-09-10 2017-06-15
10543 CulinaryArts PostSecondary PostSec 85 2013-07-02 2015-06-20
11609 Carpentry PostSecondary PostSec 85 2013-07-02 2014-06-20
12064 Hairstyling PostSecondary PostSec 108 2016-02-01 2017-06-17
12065 Automotive PostSecondary PostSec 122 2016-02-01 2018-06-22
13687 CulinaryArts PostSecondary PostSec 85 2013-09-05 2016-06-20
13855 Automotive PostSecondary PostSec 103 2015-02-03 2015-06-19
15258 Autobody Secondary PTSD 85 2013-07-02 2014-06-20
;
run;

 

Want:

 

StudentUID Programs EnrollmentStatus RegisterStatus TermCalendarID StartDate CompletionDate
785 CulinaryArts PostSecondary PostSec 108 2015-09-10 2016-06-17
2774 Autobody PostSecondary PostSec 108 2015-09-10 2016-06-30
2774 Autobody PostSecondary PostSec 108 2016-07-01 2017-06-15
10543 CulinaryArts PostSecondary PostSec 85 2013-07-02 2014-06-30
10543 CulinaryArts PostSecondary PostSec 85 2014-07-01 2015-06-20
11609 Carpentry PostSecondary PostSec 85 2013-07-02 2014-06-20
12064 Hairstyling PostSecondary PostSec 108 2016-02-01 2016-06-30
12064 Hairstyling PostSecondary PostSec 108 2016-07-01 2017-06-17
12065 Automotive PostSecondary PostSec 122 2016-02-01 2016-06-30
12065 Automotive PostSecondary PostSec 122 2016-07-01 2017-06-30
12065 Automotive PostSecondary PostSec 122 2017-07-01 2018-06-22
13687 CulinaryArts PostSecondary PostSec 85 2013-09-05 2014-06-30
13687 CulinaryArts PostSecondary PostSec 85 2014-07-01 2015-06-30
13687 CulinaryArts PostSecondary PostSec 85 2015-07-01 2016-06-20
13855 Automotive PostSecondary PostSec 103 2015-02-03 2015-06-19
15258 Autobody Secondary PTSD 85 2013-07-02 2014-06-20
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data sets2;
    length StudentUID 8 Programs $ 22 EnrollmentStatus $ 16 RegisterStatus $ 15 TermCalendarID 8 StartDate 8 CompletionDate 8 ;
    input StudentUID : BEST32. Programs : $CHAR22. EnrollmentStatus : $CHAR16. RegisterStatus : $CHAR15. TermCalendarID : BEST32. StartDate :yymmdd10. CompletionDate : yymmdd10. ;
	format StartDate yymmdd10. CompletionDate  yymmdd10. ;
datalines;
785 CulinaryArts PostSecondary PostSec 108 2015-09-10 2016-06-17
2774 Autobody PostSecondary PostSec 108 2015-09-10 2017-06-15
10543 CulinaryArts PostSecondary PostSec 85 2013-07-02 2015-06-20
11609 Carpentry PostSecondary PostSec 85 2013-07-02 2014-06-20
12064 Hairstyling PostSecondary PostSec 108 2016-02-01 2017-06-17
12065 Automotive PostSecondary PostSec 122 2016-02-01 2018-06-22
13687 CulinaryArts PostSecondary PostSec 85 2013-09-05 2016-06-20
13855 Automotive PostSecondary PostSec 103 2015-02-03 2015-06-19
15258 Autobody Secondary PTSD 85 2013-07-02 2014-06-20
;
run;

data want;
set sets2;
_CompletionDate=CompletionDate;
_k=year(_CompletionDate)-year(startdate);
if _k=1 then do;
	if StartDate<mdy(6,30,year(StartDate)) then do;
	CompletionDate=mdy(6,30,year(StartDate));
	output;
	StartDate=CompletionDate+1;
	CompletionDate=_CompletionDate;
	output;
	end;
	else output;
end;
else if _k>1 then do;
do n=year(StartDate) to year(_CompletionDate)-1;
	if n=year(StartDate) then 
	do;
		if StartDate<mdy(6,30,n) then do;
		CompletionDate=mdy(6,30,n);
		output;
		StartDate=CompletionDate+1;
		CompletionDate=mdy(6,30,n+1);
		output;
		end;
		else do; CompletionDate=mdy(6,30,n+1);output;end;
	end;
	else if year(StartDate)<n<year(_CompletionDate)-1 then 
	do;
		startdate=mdy(7,1,n);
		CompletionDate=mdy(6,30,n+1);
		output;
	end;
	else if n=year(_CompletionDate)-1 then 
	do;
		startdate=mdy(7,1,n);CompletionDate=_CompletionDate;
		output;
	end;
end;
end;
else output;
drop n _:;
run;



View solution in original post

5 REPLIES 5
Astounding
PROC Star

Here's a suggestion about where to begin.

 

You need to learn about how SAS handles dates.  What you want is not possible when you read in dates as character strings.

 

 

novinosrin
Tourmaline | Level 20
data sets2;
    length StudentUID 8 Programs $ 22 EnrollmentStatus $ 16 RegisterStatus $ 15 TermCalendarID 8 StartDate 8 CompletionDate 8 ;
    input StudentUID : BEST32. Programs : $CHAR22. EnrollmentStatus : $CHAR16. RegisterStatus : $CHAR15. TermCalendarID : BEST32. StartDate :yymmdd10. CompletionDate : yymmdd10. ;
	format StartDate yymmdd10. CompletionDate  yymmdd10. ;
datalines;
785 CulinaryArts PostSecondary PostSec 108 2015-09-10 2016-06-17
2774 Autobody PostSecondary PostSec 108 2015-09-10 2017-06-15
10543 CulinaryArts PostSecondary PostSec 85 2013-07-02 2015-06-20
11609 Carpentry PostSecondary PostSec 85 2013-07-02 2014-06-20
12064 Hairstyling PostSecondary PostSec 108 2016-02-01 2017-06-17
12065 Automotive PostSecondary PostSec 122 2016-02-01 2018-06-22
13687 CulinaryArts PostSecondary PostSec 85 2013-09-05 2016-06-20
13855 Automotive PostSecondary PostSec 103 2015-02-03 2015-06-19
15258 Autobody Secondary PTSD 85 2013-07-02 2014-06-20
;
run;

data want;
set sets2;
_CompletionDate=CompletionDate;
_k=year(_CompletionDate)-year(startdate);
if _k=1 then do;
	if StartDate<mdy(6,30,year(StartDate)) then do;
	CompletionDate=mdy(6,30,year(StartDate));
	output;
	StartDate=CompletionDate+1;
	CompletionDate=_CompletionDate;
	output;
	end;
	else output;
end;
else if _k>1 then do;
do n=year(StartDate) to year(_CompletionDate)-1;
	if n=year(StartDate) then 
	do;
		if StartDate<mdy(6,30,n) then do;
		CompletionDate=mdy(6,30,n);
		output;
		StartDate=CompletionDate+1;
		CompletionDate=mdy(6,30,n+1);
		output;
		end;
		else do; CompletionDate=mdy(6,30,n+1);output;end;
	end;
	else if year(StartDate)<n<year(_CompletionDate)-1 then 
	do;
		startdate=mdy(7,1,n);
		CompletionDate=mdy(6,30,n+1);
		output;
	end;
	else if n=year(_CompletionDate)-1 then 
	do;
		startdate=mdy(7,1,n);CompletionDate=_CompletionDate;
		output;
	end;
end;
end;
else output;
drop n _:;
run;



twildone
Pyrite | Level 9

Hi Novinosrin....It worked perfectly...thanks for your Help.....

novinosrin
Tourmaline | Level 20

@twildone My eyesight today hasn't been the best due to lack of enough sleep last night and consequently I am not in good form. Therefore, please do test thoroughly and let me know for further edit/changes if any. Once i feel better, I will review again later to make it concise or better. Have a great afternoon

art297
Opal | Level 21

While @novinosrin's code assumes that you can re-input your starting file, inputting dates as dates rather than character strings, the following does the task with the data as you originally showed it:

data sets2;
  length StudentUID 8 Programs $ 22 EnrollmentStatus $ 16 RegisterStatus $ 15 TermCalendarID 8 StartDate $ 10 CompletionDate $ 10 ;
  input StudentUID : BEST32. Programs : $CHAR22. EnrollmentStatus : $CHAR16. RegisterStatus : $CHAR15. TermCalendarID : BEST32. StartDate : $CHAR10. CompletionDate : $CHAR10. ;
  datalines;
785 CulinaryArts PostSecondary PostSec 108 2015-09-10 2016-06-17
2774 Autobody PostSecondary PostSec 108 2015-09-10 2017-06-15
10543 CulinaryArts PostSecondary PostSec 85 2013-07-02 2015-06-20
11609 Carpentry PostSecondary PostSec 85 2013-07-02 2014-06-20
12064 Hairstyling PostSecondary PostSec 108 2016-02-01 2017-06-17
12065 Automotive PostSecondary PostSec 122 2016-02-01 2018-06-22
13687 CulinaryArts PostSecondary PostSec 85 2013-09-05 2016-06-20
13855 Automotive PostSecondary PostSec 103 2015-02-03 2015-06-19
15258 Autobody Secondary PTSD 85 2013-07-02 2014-06-20
;
run;

data want (drop=i SDate CDate _:);
  set sets2 (rename=(startdate=_startdate completiondate=_completiondate));
  format StartDate CompletionDate yymmdd10.;
  SDate=input(_StartDate,yymmdd10.);
  CDate=input(_CompletionDate,yymmdd10.);
  do i=1 to intck('year.7', SDate, CDate)+1;
    if i eq 1 then do;
      StartDate=SDate;
      if i ne intck('year.7', SDate, CDate)+1 then do;
        if month(SDate) lt 7 then
         CompletionDate=mdy(6,30,year(SDate));
        else CompletionDate=mdy(6,30,year(SDate)+1);
      end;
      else CompletionDate=CDate;
    end;
    else do;
      StartDate=mdy(7,1,year(CompletionDate));
      if i ne intck('year.7', SDate, CDate)+1 then
       CompletionDate=mdy(6,30,year(StartDate)+1);
      else CompletionDate=CDate;
    end;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

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
  • 5 replies
  • 957 views
  • 2 likes
  • 4 in conversation