Hi guys,
here is my code:
%macro statut_client(data,clientid);
proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
%if class='good' %then %do;
proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
putlog 'bon client';
%end;
%else %if class="bad" %then %do;
putlog 'mauvais client';
proc print data=&data;
%end;
run;
%mend;
%statut_client(data_credit,1215693);
I have a table called "data_client" and I would like to do the following: when i run the code I want to see a particular client(by his clientID and the variables clientid duration purpose and savings_status, when the variable class=good. If the variable class=bad then I want a message that says "bad client". Can someone tell me what's wrong with my macro!
Thanks
For starters MACRO conditionals %IF do not examine data set values.
%if %then %do ;
%end;
blocks are to limit/ generate code based on macro variable values. So you need to find some way to assign either "good" or "bad" to a macro variable.
You description is a bit incomplete. Does your data ever have more than one record for the given client? Would it ever have mixed values of good and bad?
Answers to those questions will go to determining a way to assign an appropriate macro variable/value.
The logical flow you've set up will not work, and it's not a simple syntax error or issue in this case. Since we do not know your ultimate objective, it's hard to comment on what your approach should be instead.
Your macro uses data, which I assume is your input data and then a clientID, so can we assume that you want to print some data for that specific client? It looks like that's happening already, since you already have a PROC PRINT so I don't see the difference between the first and second PROC PRINT.
Perhaps using a BY GROUP processing for your PROC PRINT is all your really need? Print it out by CLASS instead?
@IliyaGB wrote:
Hi guys,
here is my code:
%macro statut_client(data,clientid);
proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
%if class='good' %then %do;
proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
putlog 'bon client';
%end;
%else %if class="bad" %then %do;
putlog 'mauvais client';
proc print data=&data;
%end;
run;
%mend;
%statut_client(data_credit,1215693);
I have a table called "data_client" and I would like to do the following: when i run the code I want to see a particular client(by his clientID and the variables clientid duration purpose and savings_status, when the variable class=good. If the variable class=bad then I want a message that says "bad client". Can someone tell me what's wrong with my macro!
Thanks
Hi,
as I said I have a table named "data_credit". That table contains columns. Here are some of the columns' names : clientID, duration, purpose, Savings_status, class(good and bad), etc. I want to create a macro named "Statut_client" that have as a parameter the clientID, i.e. when I write statut_client(1215693), where 1215693 is the clientID of 1 client from the table, I want to see as a result the clientid,purpose,savings_status of this client if his "class=good", and I want to see a message that says "bad client" of for exemple some other client I am evoking with the macro has a "class=bad".
In other words my macro has to show the values of the following 4 variables(clientid, duration, purpose and savings_status) if the value of the variable class=good and my macro has to show "bad client" if the value of the variable class=bad.
I really cannot resolve this problem, that's why I am seeking for help.
status_client(1215693) (supposing for this client class=good) then I want the values for clientID, duration, purpose and savings_status.
statut_client(312y321)(supposing for this client class=bad) then I want message "bad client" with no other information.
And you want this message put the log, the ODS window or somewhere else....?
You had three proc prints in your original code, can you elaborate on what you were trying to do there?
Is there any reason you can't just use a BY statement? Run this to see an example. You could modify your code to be similar (using STATUS variable) and then have a title output to the PDF/EXCEL/OUTPUT.
proc sort data=sashelp.class out=class; by sex; run;
options nobyline;
title "Client Status = #byval1";
proc print data=class;
where; *add your where criteria here for specific clientID;
by sex;
var name age weight;
run;
@IliyaGB wrote:
Hi,
as I said I have a table named "data_credit". That table contains columns. Here are some of the columns' names : clientID, duration, purpose, Savings_status, class(good and bad), etc. I want to create a macro named "Statut_client" that have as a parameter the clientID, i.e. when I write statut_client(1215693), where 1215693 is the clientID of 1 client from the table, I want to see as a result the clientid,purpose,savings_status of this client if his "class=good", and I want to see a message that says "bad client" of for exemple some other client I am evoking with the macro has a "class=bad".
In other words my macro has to show the values of the following 4 variables(clientid, duration, purpose and savings_status) if the value of the variable class=good and my macro has to show "bad client" if the value of the variable class=bad.
I really cannot resolve this problem, that's why I am seeking for help.
status_client(1215693) (supposing for this client class=good) then I want the values for clientID, duration, purpose and savings_status.
statut_client(312y321)(supposing for this client class=bad) then I want message "bad client" with no other information.
@IliyaGB wrote:
I want macro that shows only 1 client at the time, depending on the clientID or show the message "bad client" thats all, I don't want to create table.
Sounds like you want to first test the status of the client and based on that run different SAS code.
But you haven't shown what you want to do differently based on the status.
Let's assume that you only want to run the PROC PRINT when the status is GOOD.
%macro status_client(in,clientid);
%local status ;
proc sql noprint ;
select upcase(class) into :status trimmed
from &in
where clientid=&cientid
;
quit;
%put &=clientid &=status;
%if &status=GOOD %then %do;
proc print data=∈
var clientid duration purpose savings_status class;
where clientid=&clientid;
run;
%end;
%mend status_client;
%status_client(data_credit,1215693);
Here's a way to approach this:
%macro statut_client(data,clientid);
data _null_;
set &data;
where clientid=&clientid;
if class='good' then do;
putlog 'bon client';
call execute ("proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
run;");
end;
else if class="bad" then putlog 'mauvais client';
stop;
run;
%mend;
%statut_client(data_credit,1215693)
Notice that SAS language (not macro language) processes the data. Also, notice the STOP statement. Without it, the process repeats for each observation that passes the WHERE condition. Finally, notice that the value of CLASS must be a match (must be all lowercase letters) if the program is to produce a report.
Are you looking in the right place? PUTLOG will write to the SAS log, not to the output file. That's easy to change, if you would like the PUT message to be written to the output file instead.
Here's a variation that puts all reports in the same output file:
%macro statut_client(data,clientid);
data _null_;
set &data;
where clientid=&clientid;
file print notitles;
if class='good' then do;
put 'ClientID ' clientid 'bon client';
call execute ("proc print data=&data;
var clientid duration purpose savings_status class;
where clientid=&clientid;
run;");
end;
else if class="bad" then put 'ClientID ' clientid 'mauvais client';
stop;
run;
%mend;
%statut_client(data_credit,1215693)
If this doesn't provide what you want, you will need to be very specific about what changes need to be made.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.