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

Hi,

suppose I have the following table consisting of dates and stock prices:

DateAB
2014-01-071020
2014-01-06815

Is it possible to transform it into the following form so that I can calculate the return:

stockdateprice
A2014-01-0710
A2014-01-068
B2014-01-0720
B2014-01-0615

Thank you!!!

1 ACCEPTED SOLUTION

Accepted Solutions
TomKari
Onyx | Level 15

PROC TRANSPOSE is ideal for jobs like this!

proc sort;

by date;

proc transpose out=want(rename=(_name_=stock col1=price));

var a b;

by date;

run;

View solution in original post

8 REPLIES 8
TomKari
Onyx | Level 15

PROC TRANSPOSE is ideal for jobs like this!

proc sort;

by date;

proc transpose out=want(rename=(_name_=stock col1=price));

var a b;

by date;

run;

ilikesas
Barite | Level 11

Hi Tom, thanks for the reply!

But in my real case I have hundreds of stocks, so is it possible to put them into the var statement in a manner other than typing each one of them?

ilikesas
Barite | Level 11

I think I realized, by omitting the var statement the code includes all of the variables

TomKari
Onyx | Level 15

I think you're correct, but all I can suggest is give it a try, and post back with any problems.

Tom

ilikesas
Barite | Level 11

I actually tried it and it worked, thanks again!

stat_sas
Ammonite | Level 13

data want(drop=i a b);

retain stock date price;

set have;

array var{*} a b;

do i=1 to dim(var);

price=var{i};

stock=vname(var{i});

output;

end;

run;

proc sort data=want;

by stock desending date;

run;

ilikesas
Barite | Level 11

Hi stat@sas, thanks for the reply!

Just have a small issue and it is that in my real data I have hundreds of stocks so it will be very helpful if there was another method of putting them all in the code instead of typing each one.

Thanks !

Karthikeyan
Fluorite | Level 6

Hi,

You can group all the numeric or character variables in a data set to an array , without typing each one of them.

array stock{*}  _numeric_ ;

Just replace the array statement in the code by stat@sas . But Make sure  other than the stock variables, the remaining should be chars . If they're not char already , make them chars.

Thanks

Karthik

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1453 views
  • 3 likes
  • 4 in conversation