Hello,
I have a dataset that I am importing that has sales by ID by year where each year is a column name. I want to transpose the data but I want YEAR variable to be numeric but it looks like PROC TRANSPOSE creates it as character. Is there a way to force it to be numeric within the PROC TRANSPOSE? I did a quick search but couldn't find anything.
Here is some sample data and the PROC TRANSPOSE that I am using. It is the _NAME_ variable (renamed to YEAR) that I want to be numeric.
options validvarname=any;
data HAVE;
input ID $ "2018"n "2019"n "2020"n;
datalines;
001 1000 1500 1250
002 2000 500 1800
;
run;
proc transpose
data= HAVE
out= WANT (rename= (_NAME_ = YEAR COL1 = SALES));
by ID;
var _numeric_;
quit;Thanks in advance.
A variable name is by definition always character, and TRANSPOSE does not test all your variable names to see if all of them are (to the human eye) numeric. For this, you always need a second step:
proc transpose
data=have
out=long (rename=(COL1 = SALES))
;
by ID;
var _numeric_;
run; /* PROC TRANSPOSE needs a RUN, not a QUIT! */
data want;
set long;
year = input(_name_,4.);
drop _name_;
run;
A variable name is by definition always character, and TRANSPOSE does not test all your variable names to see if all of them are (to the human eye) numeric. For this, you always need a second step:
proc transpose
data=have
out=long (rename=(COL1 = SALES))
;
by ID;
var _numeric_;
run; /* PROC TRANSPOSE needs a RUN, not a QUIT! */
data want;
set long;
year = input(_name_,4.);
drop _name_;
run;
There are only a few procedures who need the QUIT, as they support RUN-group processing. Think DATASETS or CATALOG. But these are few and far between 😉
In addition to that, SQL is a procedure that executes each statement immediately and also needs a QUIT to terminate.
If in doubt about a certain procedure, look up its documentation to see if it supports RUN-group processing.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.