Desktop productivity for business analysts and programmers

Creating new dataset

Reply
Occasional Contributor
Posts: 9

Creating new dataset

Hi,

I have a SAS dataset like that:

 

DateSalesBonus
2018010110050
2018010810251
2018011511053

 

All of variables are numeric format. Now, I would like to create a new SAS data set from previous dataset like this:

 

 010120180108201815012018
Sales100102110
Bonus505153

 

Please note that Date variables should be date format (ddmmyyyy).

 

how can I do this?

 

Any help will be appreciated.

Super User
Super User
Posts: 9,227

Re: Creating new dataset

Posted in reply to BURHAN_CIGDEM

You cannot.  Variable names should start with an A-Z or underscore.  You can set the label to be the date and have variable name as d then date:

data have;
  input date $ sales bonus;
datalines;
20180101   100   50
20180108   102   51
20180115   110   53
;
run;
proc transpose data=have out=inter;
  by date;
  var sales bonus;
run;
proc sort data=inter;
  by _name_ date;
run;
proc transpose data=inter out=want prefix=d;
  by _name_;
  var col1;
  id date;
  idlabel date;
run;
  

Unless it is for an output file though I would really recommend against it.  You will find the data far harder to program against, just knowing what variables you have becomes a whole set of code in itself then.

Super User
Posts: 10,626

Re: Creating new dataset

Posted in reply to BURHAN_CIGDEM
data have;
  input date $ sales bonus;
datalines;
20180101   100   50
20180108   102   51
20180115   110   53
;
run;
proc transpose data=have out=want;
id date;
var sales bonus;
run;
Super User
Posts: 6,544

Re: Creating new dataset

@Ksharp's solution works, as long as you have properly set this option first:

 

options validvarname=any;

 

That's what allows you to use variable names (such as the date values) that would not normally be permitted.  Also note that you will have to refer to your variable names a little differently going forward.  This will not work:

 

var 20180101 20180108 20180115;

 

Instead, you will need to use:

 

var '20180101'n '20180108'n '20180115'n;

 

 

Super User
Super User
Posts: 9,227

Re: Creating new dataset

Posted in reply to Astounding

Which in any production environment is to be used with extreme caution.  Any programming language is based on fundamental rules, it helps with programming for instance, and this option is effectively saying do whatever you like.  Even in that simple example coding has become far harder as you need quotes, the extra character, lose the ability to array them simply, or refer to them in lists.  Its just a very bad idea. 

Ask a Question
Discussion stats
  • 4 replies
  • 186 views
  • 4 likes
  • 4 in conversation