BookmarkSubscribeRSS Feed
ssc
Calcite | Level 5 ssc
Calcite | Level 5
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 *******************************************/
2 REPLIES 2
data_null__
Jade | Level 19
[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_;
DataShare
SAS Employee
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;

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 Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1227 views
  • 0 likes
  • 3 in conversation