Mcro with if and then statements

Reply
Occasional Learner
Posts: 1

Mcro with if and then statements

Hi Guys!

I have a problem that I can't figure out, maybe you can help me out. So for example I have a data set with 6 columns for each patients name:
Mike, Mary, Anna, Dana, Olga, Paul, populated as (Yes/No). I also have a separate list for their height, weight, age.. etc. So I want to create macro with IF and THEN statements that will assign those numbers for them. Here is an example of what I am trying to do in regular code and it works:
********************************************************************;
data names;
set names;
if Mike^='No' then do; height=5'8; weight=160; age=20; end;
else if Mary^='No' then do; height=5'5; weight=110; age=20;end
else if Anna^='No' then do; height=5'7; weight=110; age=22;end
else if Dana^='No' then do; height=5'4; weight=120; age=30;end
else if Olga^='No' then do; height 5'4; weight=125; age=32;end
else if Paul^='No' then do; height 6'0; weight=200; age=40;end
run;
********************************************************************;
But what I would like to do is to put this in a macro so instead of 5 lines I would have one or two lines of code and the rest is in the mend statements below. I tried do it like this, but it didn't work:
********************************************************************;
%macro test (nme, hg, wg, ag);
data names;
set names;
if Mike^='No' then do; height=5'8; weight=160; age=20; end;
else if nme not in ('No') then do; then do; height=&hg; weight=&wg; age=&ag; end;
run;
%mend;
%test(Mary, 5'5, 110, 20);
%test(Anna, 5'7, 110, 22);
%test(Dana, 5'4, 120, 30);
%test(Olga, 5'4, 125, 32);
%test(Paul, 6'0, 200, 40);
********************************************************************;
For some reason it only picks up the first line of the code for "if Mike..." and the last line of the mend statement for "else if Paul..." , and doesn't populate height, weight, age columns for other names.

Can someone figure this out?

Thanks!

Grand Advisor
Posts: 17,338

Re: Mcro with if and then statements

I would kindly suggest reading them in via cards or datalines statements and then doing some processing via a merge statement instead.
Grand Advisor
Posts: 10,210

Re: Mcro with if and then statements

The code problem is likely related to the values like 5'5 which are unclosed quotes. if you look at the code in the SAS editor you should see some coloring that looks odd. And why do you want a measurement like height as a character?

 

Would would ask what else you are doing with this the data as for a vast majority of activities haveing the data with one observation per person is much easier to work with.

 

When working with macros you want to learn to use the debug system options such as Mprint Mlogic and Symbolgen.

If you run

Options mprint;

before the code it will show in the log the code generated by the macros.

 

Respected Advisor
Posts: 4,973

Re: Mcro with if and then statements

Is this an attempt to learn macro language?  Or is it a practical problem?

 

You cannot use macro language yet.  You claim to have a working program, but you don't.  Here are two examples:

 

  • The symbol ^ means "not".  So the first IF THEN checks whether the variable MIKE is not equal to "No".  That would include any other value such as blanks, or "N" or even "NO" or "no".  Even if the statement runs without error, it is likely generating the wrong result.
  • The assignments of values for HEIGHT are not legitimate SAS statements.

If you are determined to turn this into a macro you absolutely must have accurate, working SAS code first.  Otherwise it will be too difficult for  you to tell whether any errors are coming from the macro language code or the SAS language statements.

Ask a Question
Discussion stats
  • 3 replies
  • 245 views
  • 0 likes
  • 4 in conversation