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.
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;
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
"not working" is such an information-rich term, that i don't know where to start to suggest something useful.
Post example data for dah.nmt and the log of the step.
My previous example is evidence that the basic logic works.
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.
@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.
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?
I do diffrent operations in both datasets that`s why I load them twice.
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.