DATA Step, Macro, Functions and more

loading same datasets in one data step

Reply
Contributor
Posts: 53

loading same datasets in one data step

[ Edited ]

Hi,

I would be gratefull if anyone could tell me if is it possible to load same dataset  twice in one datastep and then output them into two sets ?

 

I ve tried code below, but both sets are loaded in the same time which results that second outputed dataset remain empty:

data zb1 zb2;
 set lib.tmp1;
  flg="flg1";
  some operations;
  if flg="flg1" then output zb1;
 set lib.tmp1;
  flg="flg2";
  some operations;
  if flg="flg2" then output zb2;
run;

 


 Thank you.

Super Contributor
Posts: 345

Re: loading same datasets in one data step

Why do you want this in one data-steps? This seems to be a bad idea, if "some operations" depends on the flg-variable. If the same operations have to be executed, you could use:

data zb1 zb2;
 set lib.tmp1;
  some operations;

  flg="flg1;
  output zb1;
  flg="flg2";
  output zb2;
run;

 


 

Super User
Posts: 7,866

Re: loading same datasets in one data step

Then you do something in your "some operations" that changes flg away from 'flg2'.

See this example:

data cl1 cl2;
set sashelp.class;
flg = 'flg1';
if flg = 'flg1' then output cl1;
set sashelp.class;
flg = 'flg2';
if flg = 'flg2' then output cl2;
run;

proc print data=cl1 noobs; run;
proc print data=cl2 noobs; run;

Result:

Name       Sex    Age    Height    Weight    flg

Alfred      M      14     69.0      112.5    flg1
Alice       F      13     56.5       84.0    flg1
Barbara     F      13     65.3       98.0    flg1
Carol       F      14     62.8      102.5    flg1
Henry       M      14     63.5      102.5    flg1
James       M      12     57.3       83.0    flg1
Jane        F      12     59.8       84.5    flg1
Janet       F      15     62.5      112.5    flg1
Jeffrey     M      13     62.5       84.0    flg1
John        M      12     59.0       99.5    flg1
Joyce       F      11     51.3       50.5    flg1
Judy        F      14     64.3       90.0    flg1
Louise      F      12     56.3       77.0    flg1
Mary        F      15     66.5      112.0    flg1
Philip      M      16     72.0      150.0    flg1
Robert      M      12     64.8      128.0    flg1
Ronald      M      15     67.0      133.0    flg1
Thomas      M      11     57.5       85.0    flg1
William     M      15     66.5      112.0    flg1
                                                 

Name       Sex    Age    Height    Weight    flg

Alfred      M      14     69.0      112.5    flg2
Alice       F      13     56.5       84.0    flg2
Barbara     F      13     65.3       98.0    flg2
Carol       F      14     62.8      102.5    flg2
Henry       M      14     63.5      102.5    flg2
James       M      12     57.3       83.0    flg2
Jane        F      12     59.8       84.5    flg2
Janet       F      15     62.5      112.5    flg2
Jeffrey     M      13     62.5       84.0    flg2
John        M      12     59.0       99.5    flg2
Joyce       F      11     51.3       50.5    flg2
Judy        F      14     64.3       90.0    flg2
Louise      F      12     56.3       77.0    flg2
Mary        F      15     66.5      112.0    flg2
Philip      M      16     72.0      150.0    flg2
Robert      M      12     64.8      128.0    flg2
Ronald      M      15     67.0      133.0    flg2
Thomas      M      11     57.5       85.0    flg2
William     M      15     66.5      112.0    flg2
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 53

Re: loading same datasets in one data step

Posted in reply to KurtBremser
I removed another operatios and my code looks excatly like this
data dostepnosc1 dostepnosc2;
set dah.nmt;
segment="Produkty kredytowe";

if segment="Produkty kredytowe" then
output dostepnosc1;
set dah.nmt;
segment="Produkty depozytowe";

if segment="Produkty depozytowe" then
output dostepnosc2;
run;
still no working
Super Contributor
Posts: 345

Re: loading same datasets in one data step

"not working" is such an information-rich term, that i don't know where to start to suggest something useful.

Contributor
Posts: 53

Re: loading same datasets in one data step

Posted in reply to andreas_lds
second output dataset remain empty.
Super User
Posts: 7,866

Re: loading same datasets in one data step

Post example data for dah.nmt and the log of the step.

My previous example is evidence that the basic logic works.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 53

Re: loading same datasets in one data step

Posted in reply to KurtBremser

I found reason why it wasn`t working.

Firstly I initialized variable segment by "Produkty kredytowe" which is shorter then "Produkty depozytowe" which results in

segment which I assume should be "Produkty depozytowe" was "Produkty depozytow", that`s why second output datasets remained empty.

Thanks.

 

Super User
Posts: 7,866

Re: loading same datasets in one data step


Matt3 wrote:

I found reason why it wasn`t working.

Firstly I initialized variable segment by "Produkty kredytowe" which is shorter then "Produkty depozytowe" which results in

segment which I assume should be "Produkty depozytowe" was "Produkty depozytow", that`s why second output datasets remained empty.

Thanks.

 


Good catch. That's why it is always a good idea to explicitly set a sufficient length for newly created variables.

 

This will end up as a new Maxim.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,173

Re: loading same datasets in one data step

@Matt3

Try to understand the sample code @KurtBremser posted.

 

Basically: You don't need to read the source data set twice. You read it once and then you're doing "stuff" with the data and you write this data to one or multiple target data sets.

 

What certainly doesn't make any sense in your code:

1. you assign a value to variable segment

2. you have an if statement which checks for the value which you just assigned - well: that's going to be always TRUE so what's the purpose of this?

Contributor
Posts: 53

Re: loading same datasets in one data step

I do diffrent operations in both datasets that`s why I load them twice.

Super User
Posts: 7,866

Re: loading same datasets in one data step

[ Edited ]

If you do not change the original contents, a reload is not necessary.

if you change the contents, the clean way for doing this is to create new variables for the target datasets, and use keep= and rename= dataset options there if you want the original column names.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,173

Re: loading same datasets in one data step

[ Edited ]

@Matt3

If you really need to read the source data twice and write the results to two target data sets then there is not much reason to not implement your logic in two data steps. That will result in much cleaner and simpler code without any impact on performance.

 

Ask a Question
Discussion stats
  • 12 replies
  • 132 views
  • 2 likes
  • 4 in conversation