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

Oh I think that I see,

the thing is that my initial data has dates in the ddmmyy10 format which I after transformed into monyy7 (because I wanted to create categories)

ilikesas
Barite | Level 11

I think that I realized why your code works good on your created data and not for me: my initial dates are in ddmmyy10. format and from them I created date2 which is in monyy7 format, so is it possible to make an accommodation for this? thanks!

Haikuo
Onyx | Level 15

Well, if there is no other twists, it should be easy to fix by just switching the informat.

data have;

input stock:$8. date:monyy7. price;

cards;

A jan2007 4

A feb2007 7

A mar2007 5

A apr2007 2

A may2007 9

B jan2007 5

B feb2007 2

B mar2007 3

B apr2007 2

B may2007 9

;

%macro retn

                (              

                dt1=

                ,dt2=

                     )

;

proc sql;

           select a.stock, (b.price-a.price)/a.price as return format=percent8.2 from have (where = (date=input("&dt1.",ddmmyy10.))) a,

                have  (where = (date=input("&dt2.",ddmmyy10.)))b

           where a.stock=b.stock;

quit;

%mend;

%retn(dt1=01-01-2007, dt2=01-05-2007)


Some other comments: it may out of line as I don't know exactly what you are trying to achieve, but in general it is not the best practice to trim down format like that, as you can see, you may, at some point, lose information and get stuck in the maze you made.

ilikesas
Barite | Level 11

Hi Hai,

my idea is exactly the same thing as you did in your have data and your code.

The only difference is that in my original data I have dates in form ddmmyy10. and for each stock there is one end-of-the-month date, but the actual day might be different for each stock: some have it on the 29th, some on the 30th and some on the 31st.

Since I also wanted to do groupings I created date2 in format monyy7. in order to discard the day

I redid your new code but still get 0 observations Smiley Sad

Thats the thing that I hate about programming, these little nuances which are hard to find and which stop the whole process!!!

Haikuo
Onyx | Level 15

Your thought of using monyy7. format is one of the options that you can try, Other solutions can include using intnx() to normalize the date before comparison. Give following another try:

data have;

input stock:$8. date:monyy7. price;

cards;

A jan2007 4

A feb2007 7

A mar2007 5

A apr2007 2

A may2007 9

B jan2007 5

B feb2007 2

B mar2007 3

B apr2007 2

B may2007 9

;

%macro retn

                (              

                dt1=

                ,dt2=

                     )

;

proc sql;

           select a.stock, (b.price-a.price)/a.price as return format=percent8.2 from have  (where = (put(date,monyy7.)="%upcase(&dt1.)")) a,

                have  (where = (put(date,monyy7.)="%upcase(&dt2.)"))b

           where a.stock=b.stock;

quit;

%mend;

%retn(dt1=jan2007, dt2=may2007)

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 19 replies
  • 2358 views
  • 6 likes
  • 6 in conversation