BookmarkSubscribeRSS Feed
KDS_1113
Obsidian | Level 7

I have a dataset I'd like to transpose:

gep2.PNG

 

I used the following code and get the table below:

proc transpose data=gep2
out=gep3
NAME=Year;
id countrycode;
var _2001-_2018;
run;

gep3.PNG

 

Isn't NAME=Year supposed to rename my first column? If not, what is the best way to rename this variable?

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16
You could use rename option like below

proc transpose data=gep2 out=gep3(rename=(_NAME_=Year));
id countrycode;
var _2001-_2018;
run;
Thanks,
Jag
KDS_1113
Obsidian | Level 7

Hi Jag - thanks for the suggestion; unfortunately, it didn't work. I keep seeing Name of Former Variable.

 

 

Ksharp
Super User
Your code is right.
What you saw is LABEL not variable NAME.
Right click the table and check column attribution .


KDS_1113
Obsidian | Level 7

Thanks, when i do a proc print it does show up correctly; however, when i try plotting I gett the same "Name of Former Variable"

 

gepplot.PNG

Reeza
Super User

Change the label, either in your proc print, graphing procedure, data step or proc datasets. 

 

label year = 'Year';

I'm surprised your graph is correct, since you have a categorical/character variable. You may want to consider switching it to a numeric variable. 

 

Your transpose is simple, perhaps consider a data step to transpose instead to move to a fully long solution? It would be easier to create graphs and such using GROUP or BY option but I'm not sure what you're doing later on. Untested because you haven't posted sample data in text form and I'm not typing out data 🙂

 

data want;
set have;

array yrs(*) _2001-_2018;

do i=1 to dim(yrs);
Value = yrs(i);
Year=input(compress(vname(yrs(i)), , 'kd'), 8.);
output;
end;


drop _2001 - _2018;
run;

Your graphing procedure would then be:

 

proc sgplot data=want;
series x=year y=value/group=countryName;
run;quit;

In general, the graphs from SGPLOT are of better quality than SAS/GRAPH procedures. 

 

 

 

PGStats
Opal | Level 21

Another way to switch to long format

 

proc transpose data=gep2
	out=gep3
	NAME=YearStr;
by CountryName CountryCode notsorted;
var _2001-_2018;
run;

data gep4;
set gep3;
Year = input(substr(yearStr, 2), 4.0);
rename col1=Value;
drop yearStr _name_;
run;

proc sgplot data=gep4;
series x=year y=value / group=CountryName;
run;

(untested)

PG

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2801 views
  • 1 like
  • 5 in conversation