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

Hello, 

My data is in format 1 and I'd like it to be in format 2. Any help you can provide on how to do it would be helpful:

 

Format 1:

DateJan-19Feb-19Mar-19
Firm1102030
Firm251545
Firm382055
Firm4922 
Firm51211 


Format 2:

Jan-19Firm110
Jan-19Firm25
Jan-19Firm38
Jan-19Firm49
Jan-19Firm512
Feb-19Firm120
Feb-19Firm215
Feb-19Firm320
Feb-19Firm422
Feb-19Firm511
Mar-19Firm130
Mar-19Firm245
Mar-19Firm355
Mar-19Firm4 
Mar-19Firm5 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

I would still suggest to use transpose, and get the periods from the labels (as these contain the original column names from the Excel):

proc sort data=ret;
by name ticker;
run;

proc transpose
  data=ret (where=(name ne ''))
  out=trans (rename=(col1=return))
;
var _:;
by name ticker;
run;

data want;
retain period name ticker return;
set trans;
format period mmyys7.;
period = input(cats('01/',_label_),ddmmyy10.);
keep period name ticker return;
run;

proc sort data=want;
by descending period name ticker;
run;

proc print data=want (obs=10) noobs;
run;

Result:

 period    name                                       ticker     return

07/2019    361 Global Equity Absolute Return I        AGRZX      0.81384
07/2019    361 Global Equity Absolute Return Inv      AGRQX      0.81466
07/2019    361 Global Equity Absolute Return Y        AGRWX      0.71138
07/2019    Calvert Absolute Return Bond A             CUBAX      0.32992
07/2019    Calvert Absolute Return Bond C             CUBCX      0.33157
07/2019    Calvert Absolute Return Bond I             CUBIX      0.34951
07/2019    Causeway Global Absolute Return Instl      CGAIX     -2.01511
07/2019    Causeway Global Absolute Return Inv        CGAVX     -2.03822
07/2019    Eaton Vance Glbl Macr Absolute Return A    EAGMX      1.44828
07/2019    Eaton Vance Glbl Macr Absolute Return C    ECGMX      1.38488

View solution in original post

9 REPLIES 9
novinosrin
Tourmaline | Level 20

data have;
infile cards  expandtabs truncover;
input Date $	Jan_19	Feb_19	Mar_19;
cards;
Firm1	10	20	30
Firm2	5	15	45
Firm3	8	20	55
Firm4	9	22	 
Firm5	12	11	 
;


proc transpose data=have out=temp;
by date;
var Jan_19--Mar_19 ;
run;

proc sort data=temp out=want(rename=(_name_=month_year col1=value));
by _name_ date;
run;
pshah38
Calcite | Level 5

Hi, I apologize but

This is the data I have: Already in a excel sheet ill be importing. This is a small subset of what i have.

NameTicker7/20196/20195/20194/2019
APSPTX1.417.47-6.684.26
BPTOAX1.347.33-6.624.24
CPSOCX1.397.23-6.724.16
DEAGMX1.451.460.560.21

 

Id like it this way:

 NameTicker 
7/2019APSPTX1.41
7/2019BPTOAX1.34
7/2019CPSOCX1.39
7/2019DEAGMX1.45
6/2019APSPTX7.47
6/2019BPTOAX7.33
6/2019CPSOCX7.23
6/2019DEAGMX1.46
5/2019APSPTX-6.68
5/2019BPTOAX-6.62
5/2019CPSOCX-6.72
5/2019DEAGMX0.56
4/2019APSPTX4.26
4/2019BPTOAX4.24
4/2019CPSOCX4.16
4/2019DEAGMX0.21

The dates along the rows not have a variable name as I had shown in my original post, so i am not able to transpose them. Could you provide help for this situation?

novinosrin
Tourmaline | Level 20

Can you please post the SAS dataset that was import from excel. I would rather use the correct SAS naming convention for variable names.

 

Btw @pshah38  It is exactly the same code to use

pshah38
Calcite | Level 5

I've attached the file. I tried the above code, but because the dates along the first row are being treated as variable name instead of observations. So i don't know how to transpose them

novinosrin
Tourmaline | Level 20

Thank you . Do you just want 4 i.e

7/2019 6/2019 5/2019 4/2019

to be considered. I see ton of missing values. 

pshah38
Calcite | Level 5
No, I want all the dates. Yes there are a lot of missing values because in those months those firms didnt have any returns.

novinosrin
Tourmaline | Level 20

Okay, I am afraid you have lot of cleaning and validating work to do, which i will leave that up to you. But the process of transpose is simple and straight forward. Once the below is done, review the dataset with some cleaning and validation process for further analysis

 

Taking variable names as-is from metadata contents

 

proc contents data=tmp1.ret /*your imported dataset*/

run;

 

Then 

 

data temp;
set tmp1.ret;
if not missing(name) and not missing(ticker);
array t __2019--__20021;
do over t;
grp=_I_;
month_year=vname(t);
value=t;
output;
end;
keep name ticker grp month_year value;
run;


proc sort data=temp out=want(drop=grp);
by grp month_year  value;
run;
Kurt_Bremser
Super User

I would still suggest to use transpose, and get the periods from the labels (as these contain the original column names from the Excel):

proc sort data=ret;
by name ticker;
run;

proc transpose
  data=ret (where=(name ne ''))
  out=trans (rename=(col1=return))
;
var _:;
by name ticker;
run;

data want;
retain period name ticker return;
set trans;
format period mmyys7.;
period = input(cats('01/',_label_),ddmmyy10.);
keep period name ticker return;
run;

proc sort data=want;
by descending period name ticker;
run;

proc print data=want (obs=10) noobs;
run;

Result:

 period    name                                       ticker     return

07/2019    361 Global Equity Absolute Return I        AGRZX      0.81384
07/2019    361 Global Equity Absolute Return Inv      AGRQX      0.81466
07/2019    361 Global Equity Absolute Return Y        AGRWX      0.71138
07/2019    Calvert Absolute Return Bond A             CUBAX      0.32992
07/2019    Calvert Absolute Return Bond C             CUBCX      0.33157
07/2019    Calvert Absolute Return Bond I             CUBIX      0.34951
07/2019    Causeway Global Absolute Return Instl      CGAIX     -2.01511
07/2019    Causeway Global Absolute Return Inv        CGAVX     -2.03822
07/2019    Eaton Vance Glbl Macr Absolute Return A    EAGMX      1.44828
07/2019    Eaton Vance Glbl Macr Absolute Return C    ECGMX      1.38488
novinosrin
Tourmaline | Level 20
data temp;
set have;
array t Jan_19	Feb_19	Mar_19;
do over t;
grp=_I_;
month_year=vname(t);
value=t;
output;
end;
run;


proc sort data=temp out=want(keep=date month_year value);
by grp month_year date value;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 9 replies
  • 1148 views
  • 1 like
  • 3 in conversation