Hi,
I have a SAS dataset like that:
Date | Sales | Bonus |
20180101 | 100 | 50 |
20180108 | 102 | 51 |
20180115 | 110 | 53 |
All of variables are numeric format. Now, I would like to create a new SAS data set from previous dataset like this:
01012018 | 01082018 | 15012018 | |
Sales | 100 | 102 | 110 |
Bonus | 50 | 51 | 53 |
Please note that Date variables should be date format (ddmmyyyy).
how can I do this?
Any help will be appreciated.
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.
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;
@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;
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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.