Hi all,
I've got a dataset with a bunch of values in a column, but I'd like to make variable in a new dataset named after those values.
For example what I have is:
data have;
input $ names;
datalines;
A
B
C
D
;
run;
And what I'd like for my end result is:
data want;
input $ A $ B $ C $ D;
datalines;
;
run;
I suppose I could use an array to grab all the values and make them variables with a do loop, but I wanted to see if anyone else had other thoughts.
I am not sure If i am understanding your requirement. However, looking at your HAVE and WANT seems like a Transpose like
data have;
input names $;
datalines;
A
B
C
D
;
run;
proc transpose data=have out=w(drop=_name_);
id names;
run;
NOTE: No variables to transpose.
NOTE: There were 4 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.W has 0 observations and 4 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
did you try PROC TRANSPOSE?
This isn't quite the same as I'm not looking to shift the form of the data from long to wide, but I'm looking to use the data's values to make new variables which are named after those values.
I am not sure If i am understanding your requirement. However, looking at your HAVE and WANT seems like a Transpose like
data have;
input names $;
datalines;
A
B
C
D
;
run;
proc transpose data=have out=w(drop=_name_);
id names;
run;
NOTE: No variables to transpose.
NOTE: There were 4 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.W has 0 observations and 4 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Ah! I was not familiar with the id statement used in proc transpose like this. This would be a lot simpler. Thank you!
Heh. I would not have expected that transpose tolerates 'no var statement' without at least a WARNING.
Sir @Kurt_Bremser I learned these plus (obs=0) in transposing variable names from the notes I have taken from excerpts of @PGStats (as we know him as PRODIGYGENIUSstats) and @Ksharp a while ago.
As a matter of fact, PG once showed how to use how to copy just metadata and create a new table using LIKE operator in a peculiar way
Example:
/*Like operator to copy metadata*/
proc sql;
create table want
like sashelp.class;
quit;
proc contents data=want;
run;
That is not the LIKE operator. It is a different syntax that just happened to use the same name as the LIKE operator.
Ah ok. Thank you.
Can you explain the use case or logic behind this?
SAS variables are not created as easily on the fly so you're likely diving into macros or arrays here at minimum.
EDIT:
Basically, it's possible to do this but it's cumbersome and tedious and there are usually many alternative ways to get a similar functionality that's easier, if you explain your use case/logic.
I am going into making a large macro. The end product is an input control file for use in a macro itself to create a baseline dataset to describe a cohort.
This is just a step in the process of that for me. I'm first creating a dataset of the files from a directory (multiple directories eventually) and then creating flag indicators of the file names themselves and eventually doing some analysis throughout all of those datasets. It's a long procedure so I tried to keep this question simple and was just looking at how others may strategize this step apart from what I was already aware of.
First, test your steps before posting here. Yours cause ERRORs.
Second, transpose is the way to go:
data have;
input names $;
datalines;
A
B
C
D
;
run;
proc transpose data=have out=trans (drop=_name_);
var names;
id names;
run;
data want;
set trans;
stop;
run;
@Kurt_Bremser To output 0 obs you can use where=false;
199 proc transpose data=have out=trans(where=(0) drop=_name_); 200 var names; 201 id names; 202 run; NOTE: There were 4 observations read from the data set WORK.HAVE. NOTE: The data set WORK.TRANS has 0 observations and 4 variables.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.