DATA Step, Macro, Functions and more

create new dataset for conditional basis

Reply
Contributor
Posts: 54

create new dataset for conditional basis

Hello Everyone! I have two dataset "master" and "child" in my program.

Condition

The child table will only be created if the count of number of record for master table >=3.

I wrote the prog below but don't know how to connect both dataset. Sorry this might be the foolish question as I did not find the solution.

data child;

input empid ename $ job $;

cards;

1001 John clerk

1002 Esten Analyst

1003 Ruakin principal

;

data master;

input pid age gender;

cards;

101 23 1

102 34 5

103 54 6

;

proc sql;

create table cnt_master

as select count(*) as cnt from master;

quit;

data final;

set cnt_master;

if cnt>=3 then output child;

run;

Thanks in advance

Super User
Posts: 10,500

Re: create new dataset for conditional basis

I'm not sure what you mean when you say connect. It would also help to provide an exampe of what you want for the final output.

Contributor
Posts: 54

Re: create new dataset for conditional basis

Thank you for looking into the matter.

Out of the two datasets (master and child), the condition is that child dataset can only be created in work library if and only if number of records in master table >=3

e.g. in the above example , As the master dataset contain only 3 record which means it satisfy the condition.

Therefore the child dataset should be created. How to write.

something like below

data master;

input pid age gender;

cards;

101 23 1

102 34 5

103 54 6 ;

proc sql;

create table cnt_master as select count(*) as cnt from master; quit;

data final;

set cnt_master;

if cnt>=3 then output child

( data child;

input empid ename $ job $;

cards;

1001 John clerk

1002 Esten Analyst

1003 Ruakin principal

; )

run;

Super Contributor
Posts: 297

Re: create new dataset for conditional basis

Is this what you are after?

OPTIONS MPRINT MLOGIC SYMBOLGEN;

DATA MASTER;

INFILE "E:\MASTER.TXT";

INPUT PID AGE GENDER;

RUN;

PROC SQL NOPRINT;

SELECT COUNT(*) INTO: MASTCNT FROM MASTER;

QUIT;

%MACRO IMPORT;

%IF &MASTCNT. >= 3 %THEN %DO;

DATA CHILD;

INFILE "E:\CHILD.TXT";

INPUT EMPID ENAME $ JOB $;

RUN;

%END;

%MEND;

%IMPORT;

Super User
Super User
Posts: 7,401

Re: create new dataset for conditional basis

Or, to save the macro part:

OPTIONS MPRINT MLOGIC SYMBOLGEN;

DATA MASTER;

INFILE "E:\MASTER.TXT";

INPUT PID AGE GENDER;

RUN;

data _null_;

     set sashelp.vtable (where=(libname="WORK" and memname="MASTER" and NOBS > 3));

     call execute('data child;

                              infile "e:\child.txt";

                              input empid ename $ job $;

                           run;');

run;

Ask a Question
Discussion stats
  • 4 replies
  • 318 views
  • 0 likes
  • 4 in conversation