Dear SAS community:
I would like to write a new observation when a dataset set presents 0 observations.
For example, conv1c is an empty dataset (nobs=0) which is checked with the following macro.
%let dsid = %sysfunc( open(conv1c) );
%global nobss;
%let nobss = %sysfunc( attrn(&dsid,nobs) );
%let rc = %sysfunc( close(&dsid) );
%put &nobss;
So far so good! the problem is now:
data conv1cc;
set conv1c;
if &nobss=0 then status=1;
run;
A similar problem has been posted here: https://communities.sas.com/t5/SAS-Programming/How-to-test-for-zero-observations-and-still-be-in-con... (but is not quite the same)
Hello,
Try
data conv1cc;
if _N_=1 and last then do;
status=1;
output;
end;
set conv1c end=last;
output;
run;
You don't need to check the number of obs with, just use nobs-option:
data work.EmptyClass;
set sashelp.class;
where Age < 10;
keep Name;
run;
data NewClass;
if 0 then set work.EmptyClass;
if _nobs = 0 then do;
Name = 'Alex';
output;
end;
set work.EmptyClass nobs=_nobs;
output;
run;
If I understand you correct, you do not need a macro.
data have;
set sashelp.class;
stop;
run;
data want;
if n = 0 then output;
set have nobs=n;
run;
@ptadgerv wrote:
Dear SAS community:
I would like to write a new observation when a dataset set presents 0 observations.
For example, conv1c is an empty dataset (nobs=0) which is checked with the following macro.
%let dsid = %sysfunc( open(conv1c) ); %global nobss; %let nobss = %sysfunc( attrn(&dsid,nobs) ); %let rc = %sysfunc( close(&dsid) ); %put &nobss;
So far so good! the problem is now:
data conv1cc; set conv1c; if &nobss=0 then status=1; run;
A similar problem has been posted here: https://communities.sas.com/t5/SAS-Programming/How-to-test-for-zero-observations-and-still-be-in-con... (but is not quite the same)
I think you may be looking for something similar to
%let dsid = %sysfunc( open(conv1c) ); %global nobss; %let nobss = %sysfunc( attrn(&dsid,nobs) ); %let rc = %sysfunc( close(&dsid) ); %put &nobss; %if &nobss = 0 %then %do; data conv1cc; set conv1c; status=1; <or what ever you mean by New observation> run; %end; %else %do; <what ever is appropriate when the count is > 0 > %end;
Since you are working with macro language then use the test of the MACRO variable to decide what code to generate.
data conv1c; set sashelp.class; stop; run; %let dsid = %sysfunc( open(conv1c) ); %global nobss; %let nobss = %sysfunc( attrn(&dsid,nlobs) ); %let rc = %sysfunc( close(&dsid) ); %put &nobss; %macro xx; %if &nobss=0 %then %do; data conv1cc; status=1;output; set conv1c; run; %end; %mend; %xx
Thanks for so many solutions!
Now, the problem is to choose the best one 😄
I will require some time to do it (please be patient, too many deadlines this week 😞 )
Today, in my problem..., I combined several of the proposed solutions in one approach. That's why is difficult to choose just one solution (but I'll do it).
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.