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
4
5
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
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;
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;
Great observation, it was a typo - it should be NE to blank, i fixed it in the query.
Best
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;
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!
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.