Hi everyone!
I am studying the macro 1 course and I am doing the exercise m104p01. Unfortunately, when I run it, the program produces a table of 12 obs, then stops and it give me the two following error messages:
I discovered that the observation the caused this error (that would e the 13th) is one in which the variable name contains an invalid character (I suppose an accent or something like this). As a confirm, If I create a database and I change all the values of name with "Iron Man" it runs perfectly. I was wondering if you know some tricks that helps me to force SAS to accept this special characters. Below you can find the code of the exercise. Thank you all 😁
/* Part b. */
%macro customers(type);
title "&type Customers";
proc sql number;
select Name, Age_Group, Type
from mc1.customers
where Type contains "&type";
quit;
title;
%mend customers;
%customers(Gold)
Either fix the dataset encoding, the SAS session encoding, or tell SAS to ignore the encoding.
select Name, Age_Group, Type
from mc1.customers (encoding='any')
where Type contains "&type"
;
It would help (now, and 100% of the time in the future) for you to show us the ENTIRE log for this macro. Please first turn on macro debugging options by running this command:
options mprint symbolgen;
Then, run the macro again, copy the ENTIRE log as text, and paste it into the window that appears when you click on the </> icon. Do NOT show us partial logs.
Hi PaigeMiller,
Thank you for the answer and for the advice! This is the log that I receive:
Please don't ignore the instructions that I gave, about using the </> icon.
Anyway, unfortunately, in this case, the full log isn't any more helpful.
Can you please show us a screen capture of that variable in the 13th record? Use the "Insert Photos" icon to include your screen capture in your reply, do not attach files.
Hi PaigeMiller,
Thank you, I have uploaded a screenshot of part of the database from Sashelp I have been working with. The program stops while evaluating the row 31, that, according to the where statement, should be put in the output dataset. If I change the value of the name it doesn't happens.
If your SAS session runs with UTF encoding, but the data contains single-byte WLATIN9 "special characters", those can be invalid, as they are only allowed as UTF continuation bytes.
Interesting, from a quick google search of the error message, it looks like this is an encoding problems (i.e. the encoding of your session doesn't match the encoding of the data, and SAS isn't able to transcode the values). See e.g. https://communities.sas.com/t5/SAS-Studio/ERROR-Invalid-characters-were-present-in-the-data/td-p/556....
Just to double check that it's not a macro problem, I would try:
proc sql number;
select Name, Age_Group, Type
from mc1.customers
where Type contains "Gold";
quit;
data want ;
set mc1.customers ;
where Type contains "Gold";
run ;
proc sql number;
select Name, Age_Group, Type
from mc1.customers ;
quit;
data want ;
set mc1.customers ;
run ;
I wonder if it is the WHERE clause that is having the problem. Do the above examples without the WHERE clause/statement work?
Hi Quentin!
Thank you very much for the replay! I run the program and the error occurs both if the script with the where statement and that without. I thinks that the cause could be the presence of non recognized symbols in the variable name, because when I change these values the problem disappears!
Can you submit:
%put %sysfunc(getoption(encoding)) ;
and post what you get back in the log?
I think you're right, the problem is likely the special characters in some of the values. If you're SAS session is using the wrong encoding, it may not be able to interpret them. When you start the SAS session, you can choose an encoding.
How are you running SAS? Is this local SAS on your PC, or SAS on a server, or Viya? Is it SAS On Demand for Academics? Depending on how you are running it, you may have varying levels of control over the encoding.
Either fix the dataset encoding, the SAS session encoding, or tell SAS to ignore the encoding.
select Name, Age_Group, Type
from mc1.customers (encoding='any')
where Type contains "&type"
;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.