Hi All, I am trying to output several different datasets in one data step, while keeping different variables in each outputted dataset and rename the variables.
Below is the sample dataset. I want to output 3 different datasets as following -
e – only keep variable "flag". No need to rename any variable.
w – only keep variable "location". No need to rename any variable.
n – keep "location" and "flag", rename "flag" to "flag1"
I tried two different approaches but both did not generate the result I want.
In the 1st approach with if then else/do, I got 3 output datasets with both "location" and "flag1". see log below.
2nd approach I tried to put keep and rename after output but I am getting syntax error. see log below.
I need to do all these in one data step for efficiency because in my real work situation, reading the data several time in different proc sql or data step is time consuming given the size of the data. Any advices or guidance are gladly appreciated.
data test6;
input Strength Width section $1. location $10.;
datalines;
1243.51 3.036 a east
1221.95 2.995 b west
1131.67 2.983 a east
1129.70 3.019 b west
1198.08 3.106 a east
1273.31 2.947 b west
1250.24 3.018 b west
1225.47 2.980 a east
1126.78 2.965 c east
1174.62 3.033 c north
1250.79 2.941 c east
1216.75 3.037 c north
;
run;
data e w n;
set test6;
if location = "east" then flag = "e";
else if location = "west" then flag = "w";
else if location = "north" then flag="n";
if flag="e" then do;
keep flag;
output e;
end;
Else if flag="w" then do;
keep location;
output w;
end;
Else if flag="n" then do;
keep location flag;
rename flag = flag1;
output n;
end;
run;
NOTE: There were 12 observations read from the data set WORK.TEST6.
NOTE: The data set WORK.E has 6 observations and 2 variables.
NOTE: The data set WORK.W has 4 observations and 2 variables.
NOTE: The data set WORK.N has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
data e2 w2 n2;
set test6;
if location = "east" then flag = "e";
if location = "west" then flag = "w";
if location = "north" then flag="n";
if flag="e" then output e2 (keep = Flag);
if flag="w" then output w2 (keep = location);
if flag="n" then output n2 (keep = location flag rename=(flag=flag1));
run;
39 GOPTIONS ACCESSIBLE;
40 data e2 w2 n2;
41 set test6;
42 if location = "east" then flag = "e";
43 if location = "west" then flag = "w";
44 if location = "north" then flag="n";
45 if flag="e" then output e2 (keep = Flag);
_
22
76
46 if flag="w" then output w2 (keep = location);
_
22
76
47 if flag="n" then output n2 (keep = location flag rename=(flag=flag1));
_
22
76
2 The SAS System 07:48 Thursday, October 24, 2019
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, RC, _DATA_, _LAST_, _NULL_.
ERROR 76-322: Syntax error, statement will be ignored.
Use dataset options when defining the datasets in the DATA statement.
data
e(keep=flag) /* only keep variable "flag". No need to rename any variable */
w(keep=location) /* only keep variable "location". No need to rename any variable. */
n(keep=location flag rename=(flag=flag1)) /* keep "location" and "flag", rename "flag" to "flag1" */
;
data e(keep=flag) w(keep=location) n(keep=location flag rename=(flag=flag1));
Use dataset options when defining the datasets in the DATA statement.
data
e(keep=flag) /* only keep variable "flag". No need to rename any variable */
w(keep=location) /* only keep variable "location". No need to rename any variable. */
n(keep=location flag rename=(flag=flag1)) /* keep "location" and "flag", rename "flag" to "flag1" */
;
I think this is what you want
data test6;
input Strength Width section $1. location $10.;
datalines;
1243.51 3.036 a east
1221.95 2.995 b west
1131.67 2.983 a east
1129.70 3.019 b west
1198.08 3.106 a east
1273.31 2.947 b west
1250.24 3.018 b west
1225.47 2.980 a east
1126.78 2.965 c east
1174.62 3.033 c north
1250.79 2.941 c east
1216.75 3.037 c north
;
data data e (keep=flag)
w (keep=location)
n (keep=location flag rename=(flag=flag1));
set test6;
flag=char(location, 1);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.