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

Please, need an assitance for this macro below, It doesn't work. I don't know why!

The aim is to alter a date character to a date numeric. Sometime, I could not I have the day of character date. So, the reference is XXday=15

 

%macro DateCharNum(VarNum, VarChar);

    if &VarChar. ne "" then do;

        if find(&VarChar., "xx/xx")>0 then &VarNum. = mdy(6, 1, substr(&VarChar., 7));

        else if find(&VarChar., "xx")>0 then &VarNum. = input(substr(&VarChar., 4), anydtdte.)+14; * 15 of each month;

        else &VarNum. = input(&VarChar., DDMMYY10.);

    end;

    else do;  &VarChar. = .;

   end;

    format &VarNum. DDMMYY10.;

%mend;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Can you provide some example input character strings with the date values you expect to create for those example strings?

 

Is the issue with the macro code?  Or the SAS code you are using the macro to generate?  Did you test the logic of the SAS code before wrapping it in a macro?

 

The macro seems to work in the sense that executes without any errors and generates SAS code that does not have any obvious syntax errors.

 

Bute the code it generates has some issues since it it treating the result of the SUBSTR() function call like it is a number and attempting to assign a numeric missing value to a character variable.

 

1977  options mprint;
1978  data test;
1979    string='12/12/2020';
1980    %datecharnum(number,string)
MPRINT(DATECHARNUM):   if string ne "" then do;
MPRINT(DATECHARNUM):   if find(string, "xx/xx")>0 then number = mdy(6, 1, substr(string, 7));
MPRINT(DATECHARNUM):   else if find(string, "xx")>0 then number = input(substr(string, 4), anydtdte.)+14;
MPRINT(DATECHARNUM):   * 15 of each month;
MPRINT(DATECHARNUM):   else number = input(string, DDMMYY10.);
MPRINT(DATECHARNUM):   end;
MPRINT(DATECHARNUM):   else do;
MPRINT(DATECHARNUM):   string = .;
MPRINT(DATECHARNUM):   end;
MPRINT(DATECHARNUM):   format number DDMMYY10.;
1981    put (_all_) (=);
1982  run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      1:97
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      2:79
string=12/12/2020 number=12/12/2020
NOTE: The data set WORK.TEST has 1 observations and 2 variables.

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Really should provide some example values of the input and the desired output value.

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages. If macros are involved set OPTIONS MPRINT; prior to executing the code so the macro resolution is shown.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

 

You may want to consider the COALESCE function in the MDY function. Pull the year, month and day from the string.

then use Coalesce with possible missing

 

date = mdy(6,coalesce(numday,15),2015);

for example if the numday is missing return 06/15/2015 (in mmddyy ) or 06/numday/2015 if a day valid for the month.

Tom
Super User Tom
Super User

Can you provide some example input character strings with the date values you expect to create for those example strings?

 

Is the issue with the macro code?  Or the SAS code you are using the macro to generate?  Did you test the logic of the SAS code before wrapping it in a macro?

 

The macro seems to work in the sense that executes without any errors and generates SAS code that does not have any obvious syntax errors.

 

Bute the code it generates has some issues since it it treating the result of the SUBSTR() function call like it is a number and attempting to assign a numeric missing value to a character variable.

 

1977  options mprint;
1978  data test;
1979    string='12/12/2020';
1980    %datecharnum(number,string)
MPRINT(DATECHARNUM):   if string ne "" then do;
MPRINT(DATECHARNUM):   if find(string, "xx/xx")>0 then number = mdy(6, 1, substr(string, 7));
MPRINT(DATECHARNUM):   else if find(string, "xx")>0 then number = input(substr(string, 4), anydtdte.)+14;
MPRINT(DATECHARNUM):   * 15 of each month;
MPRINT(DATECHARNUM):   else number = input(string, DDMMYY10.);
MPRINT(DATECHARNUM):   end;
MPRINT(DATECHARNUM):   else do;
MPRINT(DATECHARNUM):   string = .;
MPRINT(DATECHARNUM):   end;
MPRINT(DATECHARNUM):   format number DDMMYY10.;
1981    put (_all_) (=);
1982  run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      1:97
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      2:79
string=12/12/2020 number=12/12/2020
NOTE: The data set WORK.TEST has 1 observations and 2 variables.

 

 

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
  • 2 replies
  • 320 views
  • 0 likes
  • 3 in conversation