SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Creating a Dataset using if statement

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Creating a Dataset using if statement

[ Edited ]

Hi,

I am running this macro to create a dataset, but it is creating all datasets even when there is no data.

 

Data have:

datalines;

Input id var;

1   abc

2   xyz

3  abc

6 abc

7 xyz;

 

I want to create datasets based on the var;

if var= "abc"  or if var="xyz";

then create abc dataset or xyz datset but if the value ne to these or blank then no output. My macro looks like this:

%macro test (var=);

data &var;

set have;

if var NE " " then do;

if var = "&var" then output;

end;

run;

%mend test;

  

%test(var=xyz);

%test (var=abc);

%test (var=ijk);

 

It is creating dataset ijk  with 0 observations, I dont want that, I want only two datasets (abc, and xyz), but I have to run the macro for ijk because data keeps changing and next week there might be some observations = ijk.

 

Help

Thanks

 


Accepted Solutions
Solution
‎04-13-2016 01:13 PM
Super User
Posts: 5,071

Re: Creating a Dataset using if statement

[ Edited ]

I don't think your approach will allow you to conditionally create a data set.  Let me point you in a different direction.

 

proc freq data=have;

tables var / noprint out=all_vars (keep=var);

where var ne ' ';

run;

 

data _null_;

set all_vars;

call execute('data ' || var || '; set have; if var="' || var || '"; run;');

run;

 

 

View solution in original post


All Replies
Super User
Posts: 5,254

Re: Creating a Dataset using if statement

Use %if instead.
Data never sleeps
Super User
Posts: 17,748

Re: Creating a Dataset using if statement

You need to explicitly account for that. 

 

I'm assuming your current code is a sample because it won't work as is. 

 

This is a contradictory condition:

 

if var NE "&var" then do;

if var = "&var" then output;

 

 

Contributor
Posts: 64

Re: Creating a Dataset using if statement

[ Edited ]

Great observation, it was a typo - it should be NE to blank, i fixed it in the query.
Best

Solution
‎04-13-2016 01:13 PM
Super User
Posts: 5,071

Re: Creating a Dataset using if statement

[ Edited ]

I don't think your approach will allow you to conditionally create a data set.  Let me point you in a different direction.

 

proc freq data=have;

tables var / noprint out=all_vars (keep=var);

where var ne ' ';

run;

 

data _null_;

set all_vars;

call execute('data ' || var || '; set have; if var="' || var || '"; run;');

run;

 

 

Contributor
Posts: 64

Re: Creating a Dataset using if statement

Thank you - stay blessed.

It worked
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 413 views
  • 1 like
  • 4 in conversation