This may be a simple question but I want to subtract 6 days from 01/09/2012 and keep the
format of DD/MM/YYYY how would I do this. Also if I compare this with another date in the same format does SAS actually compare the dates so if I said
If (Date1<Date2) /*Does this work in SAS */
SAS dates are simply stored as the number of days since 01JAN1960 - so just subtract six :-)
See my log:
44 data _null_;
45 date1 = '01SEP2012'd;
46 date2 = date1 - 6;
47 put date2= ddmmyys10.; /* the format you need */
48 if (date1 < date2) then put 'false'; /* this DOES work in SAS */
49 else put date1= date2=; /* unformatted - num of days*/
50 run;
date2=26/08/2012
date1=19237 date2=19231
Related
I'd like to calculate the number of patients currently within an Emergency Room by hour and I'm having trouble conceptualizing an efficient code.
I have two time variables, 'Check In Time' and 'Release Time'. These date/time variables are obviously arbitrary and the 'release time' variable will come after the 'check in time variable'.
I would like the output for a given day to look something like this:
Hour Midnight 1am 2am 3am 4am.....
# of Pts 34 56 89 23 29
So for example, at 1am there were 56 patients currently in the ED -when considering both checkin and release times.
My initial thought is to:
1) round the time variables
2) Write a code a code the looks something like this...
data EDTimesl;
set EDDATA;
if checkin = '1am' and release = '2am' then OneAMToTwoAM = 1;
if checkin = '1am' and release = '3am' then OneAMToTwoAM = 1;
if checkin = '1am' and release = '3am' then TwoAMToThreeAM = 1;
....
run;
This, however, gives me pause because I feel there is a more efficient method!
Thanks in advance!
I found a code online that might answer the question! Please see below:
data have (keep=admitdate disdate);
/* generate some admission and discharge date time variables*/
year=2015; /* for example all of the admits are in 2015*/
format admitdate disdate datetime20.;
do day= 1 to 20;
do month=1 to 12;
hour = floor(24*ranuni(4445));
min = floor(50*ranuni(1234));
date = mdy(month,day,2015);
admitdate=dhms(date,hour,min,0);
/* random duration of stay*/
duration = 60 + floor(3000*ranuni(7777));
disdate = intnx('minute',admitdate,duration);
output;
end;
end;
run;
data occupancy;
set have;
format admitdate disdate datetime20.;
Do Occupanthour = (dhms(datepart(admitdate),hour(admitdate),0,0)) to
dhms(datepart(disdate),hour(disdate),0,0) by 3600;
HourOfDay = hour(OccupantHour);
DayOfWeek = Weekday(datepart(OccupantHour));
output;
end;
format OccupantHour datetime20.;
run;
Proc freq data=occupancy;
Tables HourOfDay;
run;
proc tabulate data=occupancy;
class DayOfWeek;
class HourOfDay;
tables HourOfDay,
(DayOfWeek All)*n;
run;
Hi I am beginner is sas and I need help for this question.
I want to convert 201711 to 13th Nov 2017. I cannot understand this tricky questions.
Please help and thanks in advance.
If this is for display purpose and assuming all date values are in the same format as that in your question then this should work.
First you create format to display the months:
proc format lib=work;
value mon
1 = "Jan"
2 = "Feb"
3 = "Mar"
4 = "Apr"
5 = "May"
6 = "Jun"
7 = "Jul"
8 = "Aug"
9 = "Sep"
10 = "Oct"
11 = "Nov"
12 = "Dec"
;
run;
Then you substring the month and the year from your date variable and then apply the formats.
data have;
length full_date $20;
date = 201711;
mon = input(substrn(date,5,2),best.);
yr = input(substrn(date,1,4),best.);
full_date = compbl(put(mon,mon.)||put(yr,best.));
run;
If '201711' is just some text you have to convert, then it seems the day number is missing so it will have to be added. SAS treats dates as numbers, so it is useful to convert text dates to a SAS date format. The date can then be reformatted:
data want;
have = '201711'; /* given partial date */
add_day = '13'; /* day of month to add */
full_dt = cats(have,add_day); /* join day to partial date */
num_dt = input(full_dt,yymmdd8.); /* convert to a SAS date */
text_dt = put(num_dt,date9.); /* format as desired */
run;
As you are new to SAS I have commented what each part is doing, but it would be more useful for you to understand date handling / processing in SAS, e.g. the following is a useful start:
http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p1wj0wt2ebe2a0n1lv4lem9hdc0v.htm
Say I have data called data and a variable age
and I am looking for a way to divide the variable representing age, in to a new variable showing the age group meaning,18-21,21-24 and so on.
I know how to do it with proc sql and if, but I understand there is an efficient way to do it using the round function.
Does anybody know how you can divide a numeric variable using round?
thanks.
Method1: Using If/Else
Method2: Using Proc format
I have shared the code for method2 as you already know method1
proc format;
value age
low - 9 = '0 - 9'
10 - 19 = '10 - 19'
20 - 29 = '20 - 29'
30 - 39 = '30 - 39'
40 - 49 = '40 - 49'
50 - 59 = '50 - 59'
60 - high = '60 +++'
;
run;
/**1) If you just want to look at age in bucket form **/
data abc;
set abc;
format age age.;
run;
/**2) If you want to create another variable(say age_bucket) **/
data abc;
set abc;
age_bucket = put(age,age.);
run;
Let me know in case of any queries.
I am trying to match max daily data within a month to a monthly data.
data daily;
input permno $ date ret;
datalines;
1000 19860101 88
1000 19860102 90
1000 19860201 70
1000 19860202 55
1001 19860201 97
1001 19860202 74
1001 19860203 79
1002 19860301 55
1002 19860302 100
1002 19860301 10
;
run;
data monthly;
input permno $ date ret;
datalines;
1000 19860131 1
1000 19860228 2
1000 19860331 5
1001 19860331 3
1002 19860430 4
;
run;
The result I want is the following; (I want to match daily max data to one month lag monthly data. )
1000 19860102 90 1000 19860228 2
1000 19860201 70 1000 19860331 5
1001 19860201 97 1001 19860331 3
1002 19860302 100 1002 19860430 4
Below is what I have tried so far.
I want to have maximum ret value within a month so I have created yrmon to assign same yyyymm data for the same month daily data
data a1; set daily;
yrmon=year(date)*100 + month(date);
run;
In order to choose the maximum value(here, ret) within same yrmon group for the same permno, I used code below
proc means data=a1 noprint;
class permno yrmon ;
var ret;
output out= a2 max=maxret;
run;
However, it only got me permno yrmon ret data, leaving the original date data away.
data a3;
set a2;
new=intnx('month',yrmon,1);
format date new yymmn6.;
run;
But it won't work since yrmon is no longer date format.
Thank you in advance.
Hello
I am trying to match two different sets by permno(same company) but with one month lag (eg. daily9 dataset yrmon=198601 and monthly2 dataset yrmon=198602)
it is pretty difficult to handle for me because if I just add +1 in yrmon, 198612 +1 will not be 198701 and I am confused with handling these issues.
Can anyone help?
1) informat date1/date2 yymmn6. is used to read the date in yyyymm format
2) format date1/date2 yymmn6. is used to view the date in yyyymm format
3) intnx("months",b.date2,-1) is used to join the dates with lag of 1 month
data data1;
input date1 value1;
informat date1 yymmn6.;
format date1 yymmn6.;
cards;
200101 200
200212 300
200211 400
;
run;
data data2;
input date2 value2;
informat date2 yymmn6.;
format date2 yymmn6.;
cards;
200101 3000000
200102 4000000
200301 2000000
200212 2000000
;
run;
proc sql;
create table result as
select a.*,b.date2,b.value2 from
data1 a
left join
data2 b
on a.date1 = intnx("months",b.date2,-1);
quit;
My Output:
date1 |value1 |date2 |value2
200101 |200 |200102 |4000000
200211 |400 |200212 |2000000
200212 |300 |200301 |2000000
Let me know in case of any queries.
I have a data set that contains quarterly data value. But now I want to sum the quarterly values which have the same year.
Data h :
time value
01JAN90 23
01APR90 31
01JUL90 13
01OCT90 45
01JAN91 11
01APR91 4
01JUL91 1
01OCT91 17
I want my result data like this:
time value
1990 53
1991 35
If your time variable is numeric, you can use a FORMAT statement within PROC SUMMARY to automatically extract the year as the PROC runs. (Thanks to #Joe for showing this in comments to my original answer.)
PROC SUMMARY NWAY DATA = h;
CLASS time;
FORMAT time YEAR. ;
OUTPUT
OUT = result (
KEEP = year value
)
SUM (value) =
;
RUN;