BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kaushal_Kumar
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

11 REPLIES 11
Haikuo
Onyx | Level 15

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

FriedEgg
SAS Employee

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

data_null__
Jade | Level 19

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;

FriedEgg
SAS Employee

That is a good optimization DN, thanks.

Kaushal_Kumar
Calcite | Level 5

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

Linlin
Lapis Lazuli | Level 10

you got 20 records, but the added record is not a blank record.

art297
Opal | Level 21

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;

Howles
Quartz | Level 8

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

Tom
Super User Tom
Super User

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

FriedEgg
SAS Employee

Very nice Tom

data_null__
Jade | Level 19

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1026 views
  • 2 likes
  • 8 in conversation