Hi SAS Users,
This post is of my curiosity when reading a published code for a paper relating to finance (data from CRSP)
I do not have chances to access the dataset so what I guess just a guess.
The dataset has a variable name "date". I guess this variable have the informat date9.
I saw the author does one calculation surprising me, he calculates:
ym=year(Date)*100 + month(Date);
I am not sure if you deal with "date", whether you have ever seen any application of the above code?
I am sorry for the ambiguous information (because I just have the code, no more than that), but I hope that I can hear something from your experience.
Warmest regards.
Updated: I read closer to the later code, it seems that this code is to calculate the month.
Run this code:
data _null_;
date = '08mar2021'd; /*today();*/
format date date9.;
y = year(Date);
m = month(Date);
ym=year(Date)*100 + month(Date); /* numerical value of "yeramonth", e.g. 202103 */
put (_ALL_) (=/);
run;
Purpose of that line is to create numerical variable with "yearmonth" value, e.g for "08mar2021'd you will get 202103
Bart
Run this code:
data _null_;
date = '08mar2021'd; /*today();*/
format date date9.;
y = year(Date);
m = month(Date);
ym=year(Date)*100 + month(Date); /* numerical value of "yeramonth", e.g. 202103 */
put (_ALL_) (=/);
run;
Purpose of that line is to create numerical variable with "yearmonth" value, e.g for "08mar2021'd you will get 202103
Bart
Personally, I would create a character variable with
put(date,yymmn6.)
Character is "1 function" and 6 bytes to store.
Numeric is "2 functions" but only 4 bytes to store,
data test;
format date date9.;
length ym 4;
do date = '01jan1600'd, today(), '31dec9999'd;
ym=year(Date)*100 + month(Date);
put (_ALL_) (=/);
output;
end;
run;
proc print;
run;
Possible question is: "Is comparison or sorting of numeric faster than character?"
Bart
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.