DATA Step, Macro, Functions and more

create variables from observations

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

create variables from observations

Hello.

I have a dataset with only one variable and 50 observations. The variable contains names of other I want to use to create a new dataset. Of course, these names change, depending on the steps of the processing.

Example of the dataset_1:

variable_name
--------------------
brand
colour
year
maxspeed


The new dataset to create, dynamicaly and empty, must have the following variables:

Example of dataset_2:

brand colour year maxspeed
------------------------------------------------

This is some kind of transpose. I tried some transposes but it didn't work out.

Can someone help?
Thanks a lot.

Accepted Solutions
Solution
‎12-30-2016 10:14 AM
Super User
Posts: 10,046

Re: create variables from observations

data have;
input name : $20.;
cards;
brand
colour
year
maxspeed
;
run;
proc transpose data=have out=want;
id name;
run;

View solution in original post


All Replies
Super User
Posts: 7,868

Re: create variables from observations

You need more information in your first dataset, not just names: variable type, length, formats, ...

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 22

Re: create variables from observations

Important: It's not possible to use the select variable into :var separated by ' ' because sometime we have so many columns that concatenated it exceeds the max lengrh of a macro variable.
Contributor
Posts: 22

Re: create variables from observations

Yes, you are right! I didn' think about it. SAS rookie.
Contributor
Posts: 22

Re: create variables from observations

Well ... I was checking ... In real life, the variables are all numeric.

Can I use a kind of transpose, @KurtBremser?
Super User
Posts: 19,878

Re: create variables from observations

You only have one column? What would be in the variables created, missing? A full example would be helpful. 

Respected Advisor
Posts: 4,937

Re: create variables from observations

You can also use a temporary file:

 

filename toto temp;

data _null_;
set have;
file toto;
put variable_name "=0;";
run;

data have;
%include toto;
stop;
run;
PG
Super User
Posts: 5,518

Re: create variables from observations

Here's a transpose approach:

 

data have2;

set have;

num_var = 1;

run;

proc transpose data=have2 out=want1 (drop=_name_ _label_);

var num_var;

id variable_name;

run;

data want2;

set want1;

stop;

run;

 

The transpose creates all the numeric variables with a value of 1.  The final DATA step removes that observation, but leaves the variable definitions in place.

Trusted Advisor
Posts: 1,586

Re: create variables from observations

[ Edited ]
Posted in reply to Astounding

You can do it in one step, assumed all variables are numeric:

 

data _NULL_;

    infile datalines truncover;

    input vname $;

   if _N_ = 1 then call exceute('data test;  length  ');

   if vname ne '==='       /* input data assigned to end of input */

      then call execeute('strip(vname) || ' ');

      else call excecute(' 8; delete; run;');

datalines;

brand

colour

year

maxspeed

===

; run;

run;

    

Solution
‎12-30-2016 10:14 AM
Super User
Posts: 10,046

Re: create variables from observations

data have;
input name : $20.;
cards;
brand
colour
year
maxspeed
;
run;
proc transpose data=have out=want;
id name;
run;

Contributor
Posts: 22

Re: create variables from observations

For my needs, this answer is valid.
Thanks.
New Contributor
Posts: 2

Re: create variables from observations

Thanks for this useful answer.

 

It's working.! Kudos to you.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 614 views
  • 13 likes
  • 8 in conversation