DATA Step, Macro, Functions and more

Appending to data set with 0 observations

Reply
ssc
N/A
Posts: 1

Appending to data set with 0 observations

Hi all,

I'm trying to append a single observation containing the value of a macro variable to an existing data set. My code (see below) works fine as long my input data set contains at least one observation, but fails if not.

Due to the context my program will run, I'm looking for a solution which works and
- ensures that type and length of data set variable VALUE in OUT is equal to INPUT
- does not need creating temporary data sets

Does anyone have an idea for a simple solution?

Many thanks!

Regards,
Stephan

/** sas code begin *******************************************/
%let mvar=test;

data INPUT; /*works */
length value $20;
value="obs1";
run;

data INPUT; /*works not*/
length value $20;
delete;
run;


data OUT;
set INPUT end=eof;
output;
if(eof) then do;
value=symget('mvar');
output;
end;
run;

/** sas code end *******************************************/
Respected Advisor
Posts: 3,799

Re: Appending to data set with 0 observations

[pre]
%let mvar=test;

data INPUT; /*works */
length value $20;
do _n_ = 1 to 10;
value="obs1";
output;
end;
run;

data INPUT; /*works not*/
length value $20;
delete;
run;* cancel;


data OUT;
do while(not eof);
set INPUT end=eof;
output;
end;
value=symget('mvar');
output;
stop;
run;
proc print;
run;
[/pre]

To make it more APPEND like where you just add data to the end MODIFY is a good choice.

[pre]
data input;
if 0 then modify input;
value=symget('mvar');
output;
stop;
run;
[/pre] Message was edited by: data _null_;
SAS Employee
Posts: 13

Re: Appending to data set with 0 observations

Other way,

data OUT ;
if nc eq 0 or eof then
do;
value=symget('mvar');
output;
end;
set INPUT nobs=nc end=eof;
output;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 195 views
  • 0 likes
  • 3 in conversation