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

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.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
yabwon
Amethyst | Level 16

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 935 views
  • 4 likes
  • 3 in conversation