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

Hi, 

 

I am trying to prepare data for a time-series analysis, but all my variables are stores in a single column. Currently my data looks something like this: 

 

DateStockReturns
Jan/18Stock A1%
Feb/18Stock A1%
Mar/18Stock A1%
Apr/18Stock A1%
May/18Stock A1%
Jan/18Stock B2%
Feb/18Stock B2%
Mar/18Stock B2%
Apr/18Stock B2%
May/18Stock B2%

 

Instead I want each stock to have its own column like this: 

DateStock AStock B
Jan/181%2%
Feb/181%2%
Mar/181%2%
Apr/181%2%
May/181%

2%

 

Can someone please help? 

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Community 🙂

 


data have;
input Date :monyy6. Stock $ Returns :percent.;
format Date monyy6.;
infile datalines4 dlm=',';
datalines;
Jan/18,Stock A,1%
Feb/18,Stock A,1%
Mar/18,Stock A,1%
Apr/18,Stock A,1%
May/18,Stock A,1%
Jan/18,Stock B,2%
Feb/18,Stock B,2%
Mar/18,Stock B,2%
Apr/18,Stock B,2%
May/18,Stock B,2%
;

proc sort data=have;
    by Date;
run;

proc transpose data=have out=want(drop=_:);
    by Date;
    id Stock;
    var Returns;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Community 🙂

 


data have;
input Date :monyy6. Stock $ Returns :percent.;
format Date monyy6.;
infile datalines4 dlm=',';
datalines;
Jan/18,Stock A,1%
Feb/18,Stock A,1%
Mar/18,Stock A,1%
Apr/18,Stock A,1%
May/18,Stock A,1%
Jan/18,Stock B,2%
Feb/18,Stock B,2%
Mar/18,Stock B,2%
Apr/18,Stock B,2%
May/18,Stock B,2%
;

proc sort data=have;
    by Date;
run;

proc transpose data=have out=want(drop=_:);
    by Date;
    id Stock;
    var Returns;
run;
Birgithj
Fluorite | Level 6

Hi, 

 

Thank you for your input. Unfortunately this does not solve my problem. All the stock returns are still in the same column with the only difference being, that the data is sorted by date instead of stock 😕

 

 

PeterClemmensen
Tourmaline | Level 20

This is the result from my code.

 

Date    Stock A	 Stock B
JAN18	0.01	 0.02
FEB18	0.01	 0.02
MAR18	0.01	 0.02
APR18	0.01	 0.02
MAY18	0.01	 0.02
Birgithj
Fluorite | Level 6

Very weird. 

My output looks like this even when running to code without any modifications. 

 

1
Jan/18Stock A00.01
2
Jan/18Stock B00.02
3
Feb/18Stock A00.01
4
Feb/18Stock B00.02
5
Mar/18Stock A00.01
6
Mar/18Stock B00.02
7
Apr/18Stock A00.01
8
Apr/18Stock B00.02
9
May/18Stock A00.01
10
May/18Stock B00.02

 

But thanks anyways for trying to help me.

Birgithj
Fluorite | Level 6

FYI, 

 

With a small modification I was able to get the code to work! Thank you for leading me in the right direction 🙂  The code I used was 

 

proc sort data=have;
by Date;
run;

 

proc transpose data=have out=new;
by date;
id stock;
var return;
run;

PeterClemmensen
Tourmaline | Level 20

Seems like the variable you named Returns is actually Return.

 

Anyway, glad you found your answer 🙂

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!
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
  • 690 views
  • 2 likes
  • 2 in conversation