DATA Step, Macro, Functions and more

re: create multiple observations from single observations

Accepted Solution Solved
Reply
Regular Contributor
Posts: 248
Accepted Solution

re: create multiple observations from single observations

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

Accepted Solutions
Solution
‎04-26-2018 03:08 PM
Super User
Posts: 2,045

Re: re: create multiple observations from single observations

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


All Replies
Super User
Posts: 6,901

Re: re: create multiple observations from single observations

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.

 

 

Solution
‎04-26-2018 03:08 PM
Super User
Posts: 2,045

Re: re: create multiple observations from single observations

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;



Regular Contributor
Posts: 248

Re: re: create multiple observations from single observations

Posted in reply to novinosrin

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

Super User
Posts: 2,045

Re: re: create multiple observations from single observations

@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

Super User
Posts: 8,213

Re: re: create multiple observations from single observations

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

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 194 views
  • 2 likes
  • 4 in conversation