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

Hi, 

 

I am reading through the solution for Level 2 Practice in Lesson 4 Creating New Columns with Character and Date Functions.

 

In the original file, pg1.eu_occ, the type for "YearMon" is character. Hence after substring the Year and Month, should they be character type too? 

 

But does it not need to be in numeric format for the MDY function to work?  Can anyone please help on this, thank you so much!

 

data eu_occ_total;
    set pg1.eu_occ;
    Year=substr(YearMon,1,4);
    Month=substr(YearMon,6,2);
    ReportDate=MDY(Month,1,Year);
    Total=sum(Hotel,ShortStay,Camp);
    format Hotel ShortStay Camp Total comma17.
           ReportDate monyy7.;
    keep Country Hotel ShortStay Camp ReportDate Total;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @mk133201 and welcome to the SAS Support Communities!

 

You are right. The SUBSTR function returns character values and the MDY function expects numeric arguments. Therefore, these three lines of code are inconsistent:

    Year=substr(YearMon,1,4);
    Month=substr(YearMon,6,2);
    ReportDate=MDY(Month,1,Year);

 

The code will work nevertheless because of automatic type conversion: Character arguments of the MDY function containing suitable digit strings like "2017" for Year would be converted to the corresponding numeric values. But this is bad practice because it would cause a note in the log saying

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
...

which should be avoided. The same type of note would be written to the log if Year and Month were numeric variables in dataset pg1.eu_occ, as the automatic character-to-numeric conversion would then occur in the two assignment statements.

 

A clean solution would convert the substrings explicitly by means of the INPUT function, e.g.:

Year =input(substr(YearMon,1,4),32.);
Month=input(substr(YearMon,6,2),32.);

(The informat lengths, here: 32, could also be chosen as 4 and 2, resp.)

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @mk133201 and welcome to the SAS Support Communities!

 

You are right. The SUBSTR function returns character values and the MDY function expects numeric arguments. Therefore, these three lines of code are inconsistent:

    Year=substr(YearMon,1,4);
    Month=substr(YearMon,6,2);
    ReportDate=MDY(Month,1,Year);

 

The code will work nevertheless because of automatic type conversion: Character arguments of the MDY function containing suitable digit strings like "2017" for Year would be converted to the corresponding numeric values. But this is bad practice because it would cause a note in the log saying

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
...

which should be avoided. The same type of note would be written to the log if Year and Month were numeric variables in dataset pg1.eu_occ, as the automatic character-to-numeric conversion would then occur in the two assignment statements.

 

A clean solution would convert the substrings explicitly by means of the INPUT function, e.g.:

Year =input(substr(YearMon,1,4),32.);
Month=input(substr(YearMon,6,2),32.);

(The informat lengths, here: 32, could also be chosen as 4 and 2, resp.)

mk133201
Calcite | Level 5
Many thanks for your clear explanation 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

LIBNAME 101

Follow along as SAS technical trainer Dominique Weatherspoon expertly answers all your questions about SAS Libraries.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1259 views
  • 1 like
  • 2 in conversation