BookmarkSubscribeRSS Feed
nj_sas
Calcite | Level 5
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
4 REPLIES 4
jdopdyke
Calcite | Level 5
* 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;
Cynthia_sas
SAS Super FREQ
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]
data_null__
Jade | Level 19
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]
deleted_user
Not applicable
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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1410 views
  • 0 likes
  • 5 in conversation