DATA Step, Macro, Functions and more

How to append data to an empty dataset using Data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to append data to an empty dataset using Data step

Hi Everyone,

   What I am trying to do is use an empty dataset as a "frame" (to set the variable order and lengths) and then append observations to that dataset with hardcoded data (using <variable>=<value> and output statements).  However, when I set the empty dataset and then try to add the variable and output statements, the resulting dataset always still has 0 observations.  

 

   Here is some example code, in which the "_out.WSTHP" is the original empty dataset (0 observations, but contains all of the variables that are described below) and I am trying to add an observation containing the subsequent data.  I would have thought the hardcoded lines and output statement would create an observation, but the resulting dataset is still empty.

 

Any ideas?

 

data _out.WSTHIP;
   set _out.WSTHIP;
   __StudyOID="dummy";
   __MetaDataVersionOID="1.3";
   __SubjectKey="dummy-102";
   __StudyEventOID="e_fu10y";
   __StudyEventRepeatKey="001";
   __FormOID="f_metabpar";
   __FormRepeatKey="001";
   __ItemGroupOID="g_wsthip";
   __ItemGroupRepeatKey="001";
   __LocationOID="Center.Admin";
   __UserOID="User.034";
   HIP=201;
   WSTHIP=1;
   WAIST=201;                           
   output;
run;   

Accepted Solutions
Solution
‎01-25-2017 11:58 AM
Super User
Posts: 5,505

Re: How to append data to an empty dataset using Data step

As it stands, the SET statement ends the DATA step because there are no observations to read.  Yet you need the SET statement within the DATA step to define the variables properly.  The solution is to keep the SET statement within the DATA step, yet avoid executing it.  For example, change it to:

 

if 5=4 then set _out.WSTHIP;

 

View solution in original post


All Replies
Solution
‎01-25-2017 11:58 AM
Super User
Posts: 5,505

Re: How to append data to an empty dataset using Data step

As it stands, the SET statement ends the DATA step because there are no observations to read.  Yet you need the SET statement within the DATA step to define the variables properly.  The solution is to keep the SET statement within the DATA step, yet avoid executing it.  For example, change it to:

 

if 5=4 then set _out.WSTHIP;

 

Occasional Contributor
Posts: 15

Re: How to append data to an empty dataset using Data step

Posted in reply to Astounding

Thanks for the suggestion!  That did the trick!! Smiley Happy

 

Thanks again!

Super User
Super User
Posts: 7,955

Re: How to append data to an empty dataset using Data step

Yes, thats normal.  There are no obs being read, so nothing happens in the datastep.  Either create a datastep with your one record, then append or set that with your template, or you could use proc sql; insert into out.wthip __studyoid="abc"...; quit;

Trusted Advisor
Posts: 1,566

Re: How to append data to an empty dataset using Data step

You can use next skilton code:

 

data _out.WSTHIP;
   set _out.WSTHIP end=eof;
output;
if eof then do;
.... enter here your code to assign new obs values...
output;
end;
run;
Occasional Contributor
Posts: 15

Re: How to append data to an empty dataset using Data step

Thanks for the suggestion!  I actually saw this method somewhere and tried it, but it still generated a 0 observation dataset.  I did get another suggestion in this thread that worked, but thanks anyway!!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 1187 views
  • 0 likes
  • 4 in conversation