BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AZIQ1
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
LinusH
Tourmaline | Level 20
Use %if instead.
Data never sleeps
Reeza
Super User

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;

 

 

AZIQ1
Quartz | Level 8

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

Astounding
PROC Star

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;

 

 

AZIQ1
Quartz | Level 8
Thank you - stay blessed.

It worked

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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