- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear Group members,
I have a sas table in sasuser.test_file1. If the total number of record is less then 20 for this table than I need to add blank records at the bottom of the table to make sure that the table has a total of 20 records. If there a SAS code which can automatically add required number of blank records to make the total number of records to 20.
many thanks,
Kaushal
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Would this be what you want?
data have;
set sashelp.class(obs=10);
run;
%macro test20;
data _null_;
call symputx('nobs',nobs);
stop;
set have nobs=nobs;
run;
%let diff=%eval(20-&nobs);
%if &diff >0 %then %do;
data want;
set have have(drop=_all_ obs=&diff);
run;
%end;
%mend;
%test20
proc print;run;
Haikuo
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Would this be what you want?
data have;
set sashelp.class(obs=10);
run;
%macro test20;
data _null_;
call symputx('nobs',nobs);
stop;
set have nobs=nobs;
run;
%let diff=%eval(20-&nobs);
%if &diff >0 %then %do;
data want;
set have have(drop=_all_ obs=&diff);
run;
%end;
%mend;
%test20
proc print;run;
Haikuo
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There is one flaw with the method from Hai.kuo, if the table would have fewer than 10 records you would not end up with 20 in the end, only up to twice what the input table contains up to the cutoff.
%macro filltable(sdl=,sds=,out=,nobs=);
%* sdl - SAS Library Name;
%* sds - SAS Data Set Name;
proc sql noprint;
select name
into :names separated by '2c'x
from dictionary.columns
where libname="%upcase(&sdl)"
and memname="%upcase(&sds)";
quit;
data &out;
do _n_=1 by 1 until(last);
set &sdl..&sds end=last;
output;
end;
call missing(&names);
do while(_n_<&nobs);
output;
_n_++1;
end;
stop;
run;
%mend;
%filltable(sdl=sashelp,sds=class,out=want,nobs=50);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
CALL MISSING(of _ALL_);
data class20;
set sashelp.class end=eof;
output;
if eof then do;
call missing(of _all_);
do _n_ = _n_+1 to 20;
output;
end;
end;
run;
proc print;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That is a good optimization DN, thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks guys for your responses
I just realized even if I remove the Call missing (of _all_) lines, code still works for me.
Data class20;
set sashelp.class end=eof;
output;
if eof then do;
do _n_ = _n_+1 to 20;
output;
end;
end;
run;
proc print;
run;
Rgds, KK
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you got 20 records, but the added record is not a blank record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Don't know why you would want to deviate from DN's suggested code.
If you don't care if the missing value records are at the beginning or end of the file, you could get around it with something like:
/*create some test data with 12 records*/
data class;
set sashelp.class (obs=12);
run;
data class20;
if _n_ eq 1 then do _n_ = numobs+1 to 20;
output;
end;
set class nobs=numobs;
output;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Not sure *why* one would want to do this, but here's a way based on MERGE.
proc sql ;
create table have as
select *
from sashelp.class
where length(name) EQ 5
order by sex, name ;
quit ;
data empty8 ;
do _n_ = 1 to 8 ; output ; end ;
set have(obs=0) ;
run ;
data want ;
merge empty8 have ; /*no BY*/
run ;
data empty8by ;
if 0 then set have ;
set have(keep=sex) ;
by sex ;
if first.sex ;
do _n_ = 1 to 8 ; output ; end ;
run ;
data wantby ;
merge empty8by have /* many-many merge */;
by sex ;
run ;
The first solution responds to the original post. The second generalizes to BY groups.
Interesting that one MERGE lacks a BY and the other MERGE is many-to-many. Both are generally deemed questionable.
Kaushal_Kumar wrote:
Dear Group members,
I have a sas table in sasuser.test_file1. If the total number of record is less then 20 for this table than I need to add blank records at the bottom of the table to make sure that the table has a total of 20 records. If there a SAS code which can automatically add required number of blank records to make the total number of records to 20.
many thanks,
Kaushal
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I like the merge with no by statement.
153 data a; set sashelp.class; run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.A has 19 observations and 5 variables.
154 data twenty; do _n_=1 to 20; output; end; run;
NOTE: The data set WORK.TWENTY has 20 observations and 0 variables.
155
156 data want;
157 merge twenty a;
158 run;
NOTE: There were 20 observations read from the data set WORK.TWENTY.
NOTE: There were 19 observations read from the data set WORK.A.
NOTE: The data set WORK.WANT has 20 observations and 5 variables.
159
160 proc print width=min data=want;
161 run;
NOTE: There were 20 observations read from the data set WORK.WANT.
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John M 12 59.0 99.5
11 Joyce F 11 51.3 50.5
12 Judy F 14 64.3 90.0
13 Louise F 12 56.3 77.0
14 Mary F 15 66.5 112.0
15 Philip M 16 72.0 150.0
16 Robert M 12 64.8 128.0
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0
20 . . .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Very nice Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Me like too.
You could even use an existing data set that has at least the target number of obs and drop _ALL_.
data class20;
merge sashelp.class sashelp.shoes(obs=20 drop=_all_);
run;