BookmarkSubscribeRSS Feed
rodrigobarg
Fluorite | Level 6

Hi,

 

I have the following example code:

 

Data example;

set source_data;

    if (VTYPE(&macro1.) eq 'C' and VTYPE(&macro2.) eq 'N' then do;

        d1 = variable1;

    end;

 

    else if (VTYPE(&macro1.) eq 'N' and VTYPE(&macro2.) eq 'C' then do;

        d2 = variable1;

    end;

 

    else do;

        d3 = variable1;

    end;

 

run;

 

 

But the output table has three new columns, d1, d2, d3, with the right one filled with data, and the other two empty. Why is SAS running things even if the if statement is false? E.g. if macro1 is C and macro2 is N, it outputs d1 correctly, but it also adds two empty columns d2 and d3. Since this is happening, I can't use input inside an if-statement, because if the variable is not char, it will throw an error. Can somebody help me?

 

 

Thanks!

 

 

1 REPLY 1
data_null__
Jade | Level 19

@rodrigobarg wrote:

Hi,

 

I have the following example code:

 

Data example;

set source_data;

    if (VTYPE(&macro1.) eq 'C' and VTYPE(&macro2.) eq 'N' then do;

        d1 = variable1;

    end;

 

    else if (VTYPE(&macro1.) eq 'N' and VTYPE(&macro2.) eq 'C' then do;

        d2 = variable1;

    end;

 

    else do;

        d3 = variable1;

    end;

 

run;

 

 

But the output table has three new columns, d1, d2, d3, with the right one filled with data, and the other two empty. Why is SAS running things even if the if statement is false? E.g. if macro1 is C and macro2 is N, it outputs d1 correctly, but it also adds two empty columns d2 and d3. Since this is happening, I can't use input inside an if-statement, because if the variable is not char, it will throw an error. Can somebody help me?

 

 

Thanks!

 

 


 

You cannot create or conditionally create a variable after a data step starts to execute.  You will need to use macro language or other technique to accomplish your task.

 

If you show a more complete example of your problem with example data someone will be able to help you get what you need.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 283 views
  • 2 likes
  • 2 in conversation