Help using Base SAS procedures

How can I insert rows?

Reply
Occasional Contributor
Posts: 19

How can I insert rows?

Hi ,
i have a dataset with variable "a" that has 3 values: 0, 1, 2. I want to insert rows for the missing values in a dataset like this:

data a;
input a b;
cards ;
0 1
1 2
2 3
0 1
2 5
0 3
1 4
...
...
;
run;
i want my output will be like this :

0 1
1 2
2 3
0 1
1 .
2 5
0 3
1 4
2 .
Thank you very much in advance

Simone
Occasional Contributor
Posts: 19

Re: How can I insert rows?

My solution...

data new; retain i -1;
if i=2 then i=0; else i=i+1;
set a;
if i = a then output;
else do;
a=i; b=0;output;
end;
run;

Simone
Occasional Contributor
Posts: 19

Re: How can I insert rows?

Arghhh! It doesn't work?
Help please!
Respected Advisor
Posts: 3,902

Re: How can I insert rows?

data Have;
input a b;
cards ;
0 1
1 2
2 3
0 1
2 5
1 1
2 1
1 3
;
run;

data want(keep=a b);
set have end=last;
retain counter 0 RetA RetB;
RetA=a;
RetB=b;
if counter>2 then counter=0;
do until( (counter>RetA and not last) or (last and counter>2));
if RetA=counter then
do;
a=RetA;
b=RetB;
end;
else
do;
a=counter;
call missing(b);
end;
output;
counter+1;
end;
run;

proc print data=want;
run;




HTH
Patrick
Ask a Question
Discussion stats
  • 3 replies
  • 168 views
  • 0 likes
  • 2 in conversation