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

Following table - i would like to subtract randdate and start date and convert into "Day" but if randdate = startdate then i want that time to be Day 1, rather than day 0

 

subjectidstartdateranddate
11/1/20191/1/2019
11/2/20191/1/2019
11/3/20191/1/2019
11/4/20191/1/2019

 

Something like this

 

subjectidstartdateranddategroup
11/1/20191/1/2019Day 1
11/2/20191/1/2019Day 2
11/2/20191/1/2019Day 2
11/3/20191/1/2019Day 3

 

Right now i can only do randdate - startdate and recode if subtract=0 then group = "day 1" but this is tedious

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The "tedious" way is fine, but you can see if you find this more appealing:

 

days = startdate - randdate + (startdate >= randdate);

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Is startdate a numeric variable or a character variable?

 

Is randdate a numeric variable or a character variable?

--
Paige Miller
ed_sas_member
Meteorite | Level 14

Hi @radhikaa4 

 

The solution you described is the easiest.

 

You can also define your own function using proc fmcp. It is easier to maintain if the rule is going to change in the future and if you use it in multiple datasets.

/* Function definition -> Fcalc_Day() with 2 arguments as defined below */

libname lib_fcmp "&path."; /* to be specified */

proc fcmp outlib=lib_fcmp.functions.dev;
	function Fcalc_Day(randdate, startdate);
	
	if randdate > startdate then return (startdate - randdate);
	if randdate <= startdate then return (startdate - randdate + 1);
	
	endsub;
quit;

options cmplib=lib_fcmp.functions;

/* Use case */
data have;
	input subjectid	startdate : mmddyy10. randdate : mmddyy10.;
	format startdate randdate mmddyy10.;
	cards;
1 12/31/2018 1/1/2019
1 1/1/2019 1/1/2019
1 1/2/2019 1/1/2019
1 1/3/2019 1/1/2019
1 1/4/2019 1/1/2019
;
run;

data want;
	set have;
	day = Fcalc_Day(randdate,startdate);
run;
Astounding
PROC Star

The "tedious" way is fine, but you can see if you find this more appealing:

 

days = startdate - randdate + (startdate >= randdate);

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
  • 3 replies
  • 1884 views
  • 3 likes
  • 4 in conversation