BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

dear all, 

my dataset has multiple variables each variable is repeated for multiple number of  years,

Each column in he dataset represent a variable-year. 

I have to transpose the  data based on variable by creating separate column of each variable containing all the time series

 

the present format of the dataset is as follows

Company NamePBDITA2010Depreciation2010sales2010PBDITA2011Depreciation2011sales2011……………..PBDITA2018Depreciation2018sales2018
20 Microns Ltd.238.747.51768.2256.153.32349.8 54591.13845.7
3I Infotech Ltd.649.64055195.33087.6517.55479.2 1578.871.62368.2
3M India Ltd.1648.8169.510912.81727.9173.711891.2 5308.741825597.6
3P Land Holdings Ltd.70.511.4701.741.816.6989.7 15.90.71.4
A B B India Ltd.7455.1485.162419.93618.5516.662947.7 1534.7221.62341
A B C Gas (International) Ltd.1.80.1136.33.60.1239.9 3.10.9100.8
A B C India Ltd.84.524.11442.7197.9422039.4 11.66.313.4
A B G Shipyard Ltd.6427.9524.118076.76837.6630.320770.9 156.78.4504.9

 

the required format should be as follows

 

Company NameyearPBDITADepreciationsales
20 Microns Ltd.2010238.747.51768.2
3I Infotech Ltd.2010649.64055195.3
3M India Ltd.20101648.8169.510912.8
3P Land Holdings Ltd.201070.511.4701.7
A B B India Ltd.20107455.1485.162419.9
A B C Gas (International) Ltd.20101.80.1136.3
A B C India Ltd.201084.524.11442.7
A B G Shipyard Ltd.20106427.9524.118076.7
20 Microns Ltd.2011256.153.32349.8
3I Infotech Ltd.20113087.6517.55479.2
3M India Ltd.20111727.9173.711891.2
3P Land Holdings Ltd.201141.816.6989.7
A B B India Ltd.20113618.5516.662947.7
A B C Gas (International) Ltd.20113.60.1239.9
A B C India Ltd.2011197.9422039.4
A B G Shipyard Ltd.20116837.6630.320770.9
20 Microns Ltd.201854591.13845.7
3I Infotech Ltd.20181578.871.62368.2
3M India Ltd.20185308.741825597.6
3P Land Holdings Ltd.201815.90.71.4
A B B India Ltd.20181534.7221.62341
A B C Gas (International) Ltd.20183.10.9100.8
A B C India Ltd.201811.66.313.4
A B G Shipyard Ltd.2018156.78.4504.9

 

thanks in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @srikanthyadav44   Pretty straight forward and has been asked many times in this forum

 

Keep it simple in 2 steps

 

data want;
set have;
array t(2010:2018,3) PBDITA2010--sales2018;
array j PBDITA	Depreciation	sales;
do year= 2010 to 2018;
 do _n_=1 to dim(j);
 j(_n_)=t(year,_n_);
 end;
 output;
end;
keep Company_Name	year	PBDITA	Depreciation	sales;
run;

proc sort data=want out=final_want;
 by year;
run;

Alternatively, Should memory be very sufficient

data _null_ ;
if _n_=1 then do;
   dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("year") ;
   h.definedata ("Company_Name","year","PBDITA","Depreciation","sales") ;
   h.definedone () ;
end;
set have end=z;
array t(2010:2018,3) PBDITA2010--sales2018;
array j PBDITA	Depreciation	sales;
do year= 2010 to 2018;
 do _n_=1 to dim(j);
 j(_n_)=t(year,_n_);
 end;
 rc=h.add();
end;
if z then h.output(dataset:'want');
run;

 

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Hi @srikanthyadav44   Pretty straight forward and has been asked many times in this forum

 

Keep it simple in 2 steps

 

data want;
set have;
array t(2010:2018,3) PBDITA2010--sales2018;
array j PBDITA	Depreciation	sales;
do year= 2010 to 2018;
 do _n_=1 to dim(j);
 j(_n_)=t(year,_n_);
 end;
 output;
end;
keep Company_Name	year	PBDITA	Depreciation	sales;
run;

proc sort data=want out=final_want;
 by year;
run;

Alternatively, Should memory be very sufficient

data _null_ ;
if _n_=1 then do;
   dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("year") ;
   h.definedata ("Company_Name","year","PBDITA","Depreciation","sales") ;
   h.definedone () ;
end;
set have end=z;
array t(2010:2018,3) PBDITA2010--sales2018;
array j PBDITA	Depreciation	sales;
do year= 2010 to 2018;
 do _n_=1 to dim(j);
 j(_n_)=t(year,_n_);
 end;
 rc=h.add();
end;
if z then h.output(dataset:'want');
run;

 

srikanthyadav44
Quartz | Level 8
dear Mr. novinosrin thanks a lot. your SAS code is workign excellently. it is very easy and simple.
srikanthyadav44
Quartz | Level 8

dear Mr. novinosrin thanks a lot. your SAS code is workign excellently. it is very easy and simple.

Tom
Super User Tom
Super User

You can perhaps just use two PROC TRANSPOSE steps.  Then you might not need to know how many variables or how many years are represented in the input.  As long as all of the variable names end with 4 digit year numbers.

proc transpose data=have out=step1;
  by company ;
  var _numeric_;
run;

data step2;
  set step1 ;
  year = input(substr(_name_,length(_name)-3),4.);
  _name_ = substr(_name_,1,length(_name_)-4);
run;
proc sort data=step2;
  by company year ;
run;

proc transpose data=step2 out=want ;
  by company year;
  id _name_;
  var col1;
run;
art297
Opal | Level 21

Given your example data all three proposed methods should work. The following will also work, but doesn't have the limitations of the other methods. The method doesn't have any memory constraints, will work regardless of how your variable names are constructed (i.e., even if the contain prefixes, delimiters and or suffixes), and can work with any combination of numeric and character variables.

 

The macro can be downloaded from: https://github.com/gerhard1050/Untranspose-a-Wide-File

 

Once you have and compile (i.e., run) the macro, the code you would need to solve your problem would be:

%untranspose(data=have, out=want, by=Company, id=year,
 var=PBDITA Depreciation sales)


proc sort data=want;
  by year Company;
run;

Art, CEO, AnalystFinder.com

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 787 views
  • 3 likes
  • 4 in conversation