BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10

Hi Everyone,

 

can anybody help me with this:

 

data HAVE;
format ID $2. VAR1 $4. VAR2 8.;
format VALID_FROM VALID_TO  Date9.;
input     @1  ID  
        @3    VAR1  
        @8  VAR2  
        @10 VALID_FROM 
        @16 VALID_TO
;
datalines;
AB xxxx 1 17828 19272
;
run;


data WANT;
format ID $2. VAR1 $4. VAR2 8.;
format VALID_FROM VALID_TO  Date9.;
input     @1  ID  
        @3    VAR1  
        @8  VAR2  
        @10 VALID_FROM 
        @16 VALID_TO
;
datalines;
AB xxxx 1 17828 17897
AB xxxx 1 17898 18262
AB xxxx 1 18263 18627
AB xxxx 1 18628 18992
AB xxxx 1 18993 19272

;
run;

 

I want to have as much observations as are years in between the valid_from and valid_to date and simultaneously retain the values for the variables ID, VAR1, VAR2.

 

How can I program this in SAS BASE?

 

Thanks,

FK1

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data HAVE;
format ID $2. VAR1 $4. VAR2 8.;
format VALID_FROM VALID_TO  Date9.;
input 	@1  ID  
		@3	VAR1  
		@8  VAR2  
		@10 VALID_FROM 
		@16 VALID_TO
;
datalines;
AB xxxx 1 17828 19272
run;

data want;
set have;
by id;
_VALID_TO=VALID_TO;
_year=year(VALID_TO)-year(VALID_from);
do _n=year(VALID_from) to year(_VALID_TO);
	if _n=year(VALID_from) then VALID_TO=intnx('year',VALID_from,0,'end');
	else if year(VALID_from)<_n<year(_VALID_TO) then do; VALID_from=mdy(1,1,_n);VALID_TO=intnx('year',mdy(1,1,_n),0,'end');end;
	else if _n=year(_VALID_TO) then do;VALID_from=mdy(1,1,_n);VALID_TO=_VALID_TO;end;
	output;
end;
format _VALID_TO date9.;
drop _:;
run;
	

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
data HAVE;
format ID $2. VAR1 $4. VAR2 8.;
format VALID_FROM VALID_TO  Date9.;
input 	@1  ID  
		@3	VAR1  
		@8  VAR2  
		@10 VALID_FROM 
		@16 VALID_TO
;
datalines;
AB xxxx 1 17828 19272
run;

data want;
set have;
by id;
_VALID_TO=VALID_TO;
_year=year(VALID_TO)-year(VALID_from);
do _n=year(VALID_from) to year(_VALID_TO);
	if _n=year(VALID_from) then VALID_TO=intnx('year',VALID_from,0,'end');
	else if year(VALID_from)<_n<year(_VALID_TO) then do; VALID_from=mdy(1,1,_n);VALID_TO=intnx('year',mdy(1,1,_n),0,'end');end;
	else if _n=year(_VALID_TO) then do;VALID_from=mdy(1,1,_n);VALID_TO=_VALID_TO;end;
	output;
end;
format _VALID_TO date9.;
drop _:;
run;
	
FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10
Thanks, novinosrin! It works perfectly fine.

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
  • 2 replies
  • 683 views
  • 0 likes
  • 2 in conversation