Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- How to insert a row/observation within a table?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-14-2016 12:17 PM
(1035 views)

**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

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.