Help using Base SAS procedures

How to insert a row/observation within a table?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

How to insert a row/observation within a table?

How to insert a row/observation within a table?

And by the meantime, I also filled the whole table with observations for the whole time span. 

Say, in the example, for each ID, I want to creat the state for every time (1 2 3 4 5 6 7). If there is a new state, just follow it. If there is no new state, follow the last state observed.

 

 

I have searched some codes, but they all add new rows to the end of the table. Thanks you for your help in advance.

 

 

 

data have;

input ID $ time $ state;

cards;

a 1 23

a 5 25

b 1 65

b 2 33

b 7 28

c 2 13

;

run;

 

 

So the data output I get in mind is that:

 

a 1 23

a 2 23

a 3 23

a 4 23

a 5 25

a 6 25

a 7 25

b 1 65

b 2 33

b 3 33

b 4 33

b 5 33

b 6 33

b 7 28

c 1 .(no state observed yet)

c 2 13

c 3 13

c 4 13

c 5 13

c 6 13

c 7 13


Accepted Solutions
Solution
‎03-14-2016 02:09 PM
Respected Advisor
Posts: 3,799

Re: How to insert a row/observation within a table?

Posted in reply to Laoban_James

This is the method I like to use.

 

data have;
   input ID $ time state;
   cards;
a 1 23
a 5 25
b 1 65
b 2 33
b 7 28
c 2 13
;;;;
   run;
proc print;
   run;
data class;
   do time=1 to 7;
      output;
      end;
   run;
proc summary nawy classdata=class data=have;
   by id;
   class time;
   output out=framed(drop=_:) idgroup(out(state)=);
   run;
data filled;
   update framed(obs=0) framed;
   by id;
   output;
   run;
proc print;
   run;

Capture.PNG

View solution in original post


All Replies
Super User
Posts: 19,858

Re: How to insert a row/observation within a table?

Posted in reply to Laoban_James

Your first issue is the missing levels - which is a commonly asked question.

http://www.pharmasug.org/proceedings/2012/CC/PharmaSUG-2012-CC26.pdf

 

Does your data have an observation for every level at some point? If so, Proc Freq with SPARSE option can help you build your table.

 

The second will have to do with retaining previous values which you can do in a data step. 

Solution
‎03-14-2016 02:09 PM
Respected Advisor
Posts: 3,799

Re: How to insert a row/observation within a table?

Posted in reply to Laoban_James

This is the method I like to use.

 

data have;
   input ID $ time state;
   cards;
a 1 23
a 5 25
b 1 65
b 2 33
b 7 28
c 2 13
;;;;
   run;
proc print;
   run;
data class;
   do time=1 to 7;
      output;
      end;
   run;
proc summary nawy classdata=class data=have;
   by id;
   class time;
   output out=framed(drop=_:) idgroup(out(state)=);
   run;
data filled;
   update framed(obs=0) framed;
   by id;
   output;
   run;
proc print;
   run;

Capture.PNG

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 327 views
  • 5 likes
  • 3 in conversation