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

Could you please guide me to resolve the error? KNDATLD is a character variable.

 

Code:

 

 

%let reporting_date=30jun2018;
data test;
KNDATLD= ' ';output;
run;

data tmp;
set test;
If KNDATLD=' ' then &reporting_date.;
else KNDATLD;
run;

Log:

 

 

 

26         %let reporting_date=30jun2018;
27         data test;
28         KNDATLD= ' ';output;
29         run;

NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

30         
31         data tmp;
32         set test;
33         If KNDATLD=' ' then &reporting_date.;
NOTE: Line generated by the macro variable "REPORTING_DATE".
2                                                          The SAS System                           09:35 Monday, September 24, 2018

33          30jun2018
            __
            180
34         else KNDATLD;
                _______
                180
ERROR 180-322: Statement is not valid or it is used out of proper order.

35         run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TMP may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
WARNING: Data set WORK.TMP was not replaced because this step was stopped.

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

@andreas_lds wrote:

Sorry, my fault. Automatically added the "d", because i hardly ever have dates as text.


Of course, as you don't define the length of the variable it is set to 1 when assigning the blank.

View solution in original post

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Macro is a find and replace system nothing more, and works before code is executed.  The simplest way to see what is going wrong is to copy and paste the macro code into your SAS code - that is the code that gets executed - then see if it is valid SAS.  So lets take your example:

data tmp;
  set test;
  if kndatld=' ' then 30jun2018;
  else kndatld;
run;

Does this look like valid Base SAS?  No, you have a then statement, with just a set of characters after it, no asignement, no string delimiters, and then an else, again with no delimiter or reference of what to do.  Lets assume that you want to assign xyz to a value:

data tmp;
  set test;
  if kndatld=' ' then xyz="30jun2018";
  else xyz=kndatld;
run;

 

 

Kurt_Bremser
Super User

Once again:

 

The macro preprocessor is a code generator. See it as a simple text replacement system. That's exactly what happens. You have to put valid SAS code into your macro variable for this to work.

Start with valid SAS code, then put the code snippet into the macrovar.

 

data tmp;
set test;
If KNDATLD=' ' then 30jun2018;
else KNDATLD;
run;

is very simply NOT valid SAS code.

andreas_lds
Jade | Level 19

You need an assignment after then and you need to enclose macro-variables in double-quotes if they contain text. The else-statement could be skipped, if you want to change the value of kndatld.

 

data tmp;
  set test;

  if missing(KNDATLD) then KNDATLD = "&reporting_date."d;
run;
Babloo
Rhodochrosite | Level 12

I ran the following code but I'm not getting the desired result.

 

code:

 

%let reporting_date=30jun2018;

data test;
KNDATLD= ' ';
output;
run;

data tmp;
set test;

if missing(KNDATLD) then
KNDATLD = "&reporting_date."d;
run;

 

Log:

 


26         %let reporting_date=30jun2018;
27         
28         data test;
29         	KNDATLD= ' ';
30         	output;
31         run;

NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

32         
33         data tmp;
34         	set test;
2                                                          The SAS System                           11:13 Monday, September 24, 2018

35         
36         	if missing(KNDATLD) then
37         		KNDATLD = "&reporting_date."d;
38         run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      37:2   
NOTE: Invalid character data, 21365.00 , at line 37 column 2.
KNDATLD=* _ERROR_=1 _N_=1
NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TMP has 1 observations and 1 variables.
NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
NOTE: DATA statement used (Total process time):

Desired Result:

 

30Jun2018 (should be in character)

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Log!

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      37:2   
NOTE: Invalid character data, 21365.00 , at line 37 column 2.

Clue is there, "..."d = date literal = numeric, variable is text.  Remove the d to have a text string. 

Babloo
Rhodochrosite | Level 12

if I remove "d", I'm getting only output as "3"

RW9
Diamond | Level 26 RW9
Diamond | Level 26
%let reporting_date=30jun2018;

data test;
length kndatld $20;
kndatld='';
output;
run;

data tmp;
set test;
if kndatld='' then kndatld="&reporting_date.";
run;

 

andreas_lds
Jade | Level 19

Sorry, my fault. Automatically added the "d", because i hardly ever have dates as text.

andreas_lds
Jade | Level 19

@andreas_lds wrote:

Sorry, my fault. Automatically added the "d", because i hardly ever have dates as text.


Of course, as you don't define the length of the variable it is set to 1 when assigning the blank.

Kurt_Bremser
Super User

@Babloo wrote:

Desired Result:

 

30Jun2018 (should be in character)


And what for would you want to have that unusable string? Store dates as SAS dates, everything else only causes additional work when needed.

No place in the world uses the date9. format for display in reports, it's either a ddmmyy, a mmddyy or a yymmdd format.

Kurt_Bremser
Super User

On second thought, it looks like you are mixing data step with SQL code.

proc sql;
create table tmp as
select
  case
    when KNDATLD=' '
    then "&reporting_date."d;
    else KNDATLD
  end as KNDATLD
from test;
quit;

would work. Note how the macrovar is enclosed in "..."d!

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
  • 11 replies
  • 3602 views
  • 0 likes
  • 4 in conversation