Macro variable

Reply
Occasional Contributor
Posts: 7

Macro variable

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

Super User
Posts: 13,542

Re: Macro variable

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.

 

 

Occasional Contributor
Posts: 7

Re: Macro variable

In my table I have column called "clientID" and another column called "class". In clientID column is the unique number of the client, for exemple 1215693, in column called "class" you can find "bad" or "good" depending on the client. One client can have only bad or good, one client cannot have both bad and good.
Super User
Posts: 23,724

Re: Macro variable

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


 

 

 

 

Occasional Contributor
Posts: 7

Re: Macro variable

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.

 

 

Super User
Posts: 23,724

Re: Macro variable

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.

 

 


 

Occasional Contributor
Posts: 7

Re: Macro variable

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.
Super User
Super User
Posts: 8,111

Re: Macro variable


@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);
Super User
Posts: 6,775

Re: Macro variable

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.

Occasional Contributor
Posts: 7

Re: Macro variable

Posted in reply to Astounding
Hi, this code works when class=good , but if it happens that for a client class=bad then it doesn't show "bad client". Also it doesn't show good client when class=good for a client/
Super User
Posts: 6,775

Re: Macro variable

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.

Occasional Contributor
Posts: 7

Re: Macro variable

Posted in reply to Astounding
Hi,
I cannot see in the Log. I just want a macro that when I write %statut_client(data_credit,111111) to have as a result all the variables like clientid,duration, purpose, and savings_status if the variable class=good for this client, if the variable class happens to be class=bad, then when I write the macro %statut_client(data_credit,222222) to see the message in the result sayint "bad client". That''s all. I dont want information in Log, just in the result page. Thanks very much for trying to help me!
Super User
Posts: 6,775

Re: Macro variable

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.

Ask a Question
Discussion stats
  • 12 replies
  • 187 views
  • 0 likes
  • 5 in conversation