BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
solfay243
Obsidian | Level 7

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

View solution in original post

15 REPLIES 15
novinosrin
Tourmaline | Level 20

did you try PROC TRANSPOSE?

solfay243
Obsidian | Level 7

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.

novinosrin
Tourmaline | Level 20

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

solfay243
Obsidian | Level 7

Ah! I was not familiar with the id statement used in proc transpose like this. This would be a lot simpler. Thank you!

novinosrin
Tourmaline | Level 20

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;

 

 

 

 

Tom
Super User Tom
Super User

That is not the LIKE operator.  It is a different syntax that just happened to use the same name as the LIKE operator.

novinosrin
Tourmaline | Level 20

Ah ok. Thank you.

Reeza
Super User

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. 

https://en.wikipedia.org/wiki/XY_problem

solfay243
Obsidian | Level 7

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.

Reeza
Super User
SAS BY group and CLASS statements usually mean you don't need to dummy code your own variables. And even if you do need to create dummy codes, glmselect can help automate that.
Kurt_Bremser
Super User

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;
data_null__
Jade | Level 19

@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.

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 15 replies
  • 1702 views
  • 6 likes
  • 7 in conversation