DATA Step, Macro, Functions and more

flag dataset name

Reply
Occasional Contributor
Posts: 10

flag dataset name

I am looking for an alternative to make below program in one datastep or any shorter. I've got mulitple datasets and trying to append all tables in one step.

data test1; set sashelp.class; flag = '30 days';run;
data test2; set sashelp.class; flag = '90 days';run;
data test3; set sashelp.class; flag = 'over all';run;
data all;
set test1 test2 test3;run;

Thanks
Occasional Contributor
Posts: 13

Re: flag dataset name

* many ways to do it. Simple approach below;

data all;
set test1(in=intest1) test2(in=intest2) test3(in=intest3) ;
if intest1 then flag='30 days';
else if intest2 then flag='90 days';
else if intest3 then flag='over all';
run;
SAS Super FREQ
Posts: 8,866

Re: flag dataset name

Hi:
There is no real reason to create TEST1, TEST2 and TEST3 unless your production process needs all 3 datasets. You could just create ALL with 1 DATA step program as shown in the comparison of your original code with the alternative code (if you want to create ALL from the same input dataset (such as SASHELP.CLASS).

If TEST1, TEST2 and TEST3 already exist then you can use the previous suggestion with the IN= option to set the value of the FLAG variable.

cynthia
[pre]
** Your ORIGINAL method;
9781 data test1; set sashelp.class; flag = '30 days '; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST1 has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


9782 data test2; set sashelp.class; flag = '90 days '; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST2 has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


9783 data test3; set sashelp.class; flag = 'over all'; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST3 has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


9784
9785 data all;
9786 set test1 test2 test3;
9787 run;

NOTE: There were 19 observations read from the data set WORK.TEST1.
NOTE: There were 19 observations read from the data set WORK.TEST2.
NOTE: There were 19 observations read from the data set WORK.TEST3.
NOTE: The data set WORK.ALL has 57 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds


** ALTERNATIVE Method creates WORK.ALL2;
9788
9789
9790 data all2;
9791 set sashelp.class;
9792 length flag $8;
9793 ** 1 obs for 30 days;
9794 flag='30 days';
9795 output;
9796
9797 ** 1 obs for 90 days;
9798 flag = '90 days';
9799 output;
9800
9801 ** 1 obs for overall;
9802 flag='over all';
9803 output;
9804 run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.ALL2 has 57 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 second

[/pre]
Respected Advisor
Posts: 3,799

Re: flag dataset name

I think you would be interested in the 9.2 SET statement option

[pre]
INDSNAME=variable
creates and names a variable that stores the name of the SAS data set from
which the current observation is read. The stored name can be a data set name or
a physical name. The physical name is the name by which the operating
environment recognizes the file.
[/pre]
N/A
Posts: 0

Re: flag dataset name

Hello,

If you need to create the temporary data sets yo may find the next macro solution useful:

[pre]
%macro a;

%do i=1 %to 3;

data test&i;
set sashelp.class;

array f{3} $ 20 _temporary_ ('30 days','90 days','over all');

flag=f{"&i"};

run;

%end;

data all;
set test:;
run;

%mend a;

%a
[/pre]

Marius
Ask a Question
Discussion stats
  • 4 replies
  • 435 views
  • 0 likes
  • 5 in conversation