BookmarkSubscribeRSS Feed
Matt3
Quartz | Level 8

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.

12 REPLIES 12
andreas_lds
Jade | Level 19

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;

 


 

Kurt_Bremser
Super User

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
Matt3
Quartz | Level 8
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
andreas_lds
Jade | Level 19

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

Matt3
Quartz | Level 8
second output dataset remain empty.
Matt3
Quartz | Level 8

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.

 

Kurt_Bremser
Super User

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

Patrick
Opal | Level 21

@Matt3

Try to understand the sample code @Kurt_Bremser 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?

Matt3
Quartz | Level 8

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

Kurt_Bremser
Super User

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.

Patrick
Opal | Level 21

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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12 replies
  • 1597 views
  • 2 likes
  • 4 in conversation