Help using Base SAS procedures

reducing the size of my code

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

reducing the size of my code

I have this rather long peice of code and was wondering how it could be reduced down to take up less lines of code.

data home.euro_f_1109;

  set home.euro_f_1109;

  if trade_date_time <= "22JUN11:16:00:00"dt then group=1;

   else if "22JUN11:17:00:00"dt <= trade_date_time <= "23JUN11:16:00:00"dt then group=2;

   else if "23JUN11:17:00:00"dt <= trade_date_time <= "24JUN11:16:00:00"dt then group=3;

   else if "24JUN11:17:00:00"dt <= trade_date_time <= "25JUN11:00:00:00"dt then group=4;

   else if "26JUN11:17:00:00"dt <= trade_date_time <= "27JUN11:16:00:00"dt then group=5;

   else if "27JUN11:17:00:00"dt <= trade_date_time <= "28JUN11:16:00:00"dt then group=6;

   else if "28JUN11:17:00:00"dt <= trade_date_time <= "29JUN11:16:00:00"dt then group=7;

   else if "29JUN11:17:00:00"dt <= trade_date_time <= "30JUN11:16:00:00"dt then group=8;

   else if "30JUN11:17:00:00"dt <= trade_date_time <= "01JUL11:16:00:00"dt then group=9;

   else if "01JUL11:17:00:00"dt <= trade_date_time <= "02JUL11:00:00:00"dt then group=10;

   else if "05JUL11:00:00:00"dt <= trade_date_time <= "05JUL11:16:00:00"dt then group=11;

   else if "05JUL11:17:00:00"dt <= trade_date_time <= "06JUL11:16:00:00"dt then group=12;

   else if "06JUL11:17:00:00"dt <= trade_date_time <= "07JUL11:16:00:00"dt then group=13;

   else if "07JUL11:17:00:00"dt <= trade_date_time <= "08JUL11:16:00:00"dt then group=14;

   else if "08JUL11:17:00:00"dt <= trade_date_time <= "09JUL11:00:00:00"dt then group=15;

   else if "10JUL11:17:00:00"dt <= trade_date_time <= "11JUL11:16:00:00"dt then group=16;

   else if "11JUL11:17:00:00"dt <= trade_date_time <= "12JUL11:16:00:00"dt then group=17;

   else if "12JUL11:17:00:00"dt <= trade_date_time <= "13JUL11:16:00:00"dt then group=18;

   else if "13JUL11:17:00:00"dt <= trade_date_time <= "14JUL11:16:00:00"dt then group=19;

   else if "14JUL11:17:00:00"dt <= trade_date_time <= "15JUL11:16:00:00"dt then group=20;

   else if "15JUL11:17:00:00"dt <= trade_date_time <= "16JUL11:00:00:00"dt then group=21;

   else if "17JUL11:17:00:00"dt <= trade_date_time <= "18JUL11:16:00:00"dt then group=22;

   else if "18JUL11:17:00:00"dt <= trade_date_time <= "19JUL11:16:00:00"dt then group=23;

   else if "19JUL11:17:00:00"dt <= trade_date_time <= "20JUL11:16:00:00"dt then group=24;

   else if "20JUL11:17:00:00"dt <= trade_date_time <= "21JUL11:16:00:00"dt then group=25;

   else if "21JUL11:17:00:00"dt <= trade_date_time <= "22JUL11:16:00:00"dt then group=26;

   else if "22JUL11:17:00:00"dt <= trade_date_time <= "23JUL11:00:00:00"dt then group=27;

   else if "24JUL11:17:00:00"dt <= trade_date_time <= "25JUL11:16:00:00"dt then group=28;

   else if "25JUL11:17:00:00"dt <= trade_date_time <= "26JUL11:16:00:00"dt then group=29;

   else if "26JUL11:17:00:00"dt <= trade_date_time <= "27JUL11:16:00:00"dt then group=30;

   else if "27JUL11:17:00:00"dt <= trade_date_time <= "28JUL11:16:00:00"dt then group=31;

   else if "28JUL11:17:00:00"dt <= trade_date_time <= "29JUL11:16:00:00"dt then group=32;

   else if "29JUL11:17:00:00"dt <= trade_date_time <= "30JUL11:00:00:00"dt then group=33;

   else if "31JUL11:17:00:00"dt <= trade_date_time <= "01AUG11:16:00:00"dt then group=34;

   else if "01AUG11:17:00:00"dt <= trade_date_time <= "02AUG11:16:00:00"dt then group=35;

   else if "02AUG11:17:00:00"dt <= trade_date_time <= "03AUG11:16:00:00"dt then group=36;

   else if "03AUG11:17:00:00"dt <= trade_date_time <= "04AUG11:16:00:00"dt then group=37;

   else if "04AUG11:17:00:00"dt <= trade_date_time <= "05AUG11:16:00:00"dt then group=38;

   else if "05AUG11:17:00:00"dt <= trade_date_time <= "06AUG11:00:00:00"dt then group=39;

   else if "07AUG11:17:00:00"dt <= trade_date_time <= "08AUG11:16:00:00"dt then group=40;

   else if "08AUG11:17:00:00"dt <= trade_date_time <= "09AUG11:16:00:00"dt then group=41;

   else if "09AUG11:17:00:00"dt <= trade_date_time <= "10AUG11:16:00:00"dt then group=42;

   else if "10AUG11:17:00:00"dt <= trade_date_time <= "11AUG11:16:00:00"dt then group=43;

   else if "11AUG11:17:00:00"dt <= trade_date_time <= "12AUG11:16:00:00"dt then group=44;

   else if "12AUG11:17:00:00"dt <= trade_date_time <= "13AUG11:00:00:00"dt then group=45;

   else if "14AUG11:17:00:00"dt <= trade_date_time <= "15AUG11:16:00:00"dt then group=46;

   else if "15AUG11:17:00:00"dt <= trade_date_time <= "16AUG11:16:00:00"dt then group=47;

   else if "16AUG11:17:00:00"dt <= trade_date_time <= "17AUG11:16:00:00"dt then group=48;

   else if "17AUG11:17:00:00"dt <= trade_date_time <= "18AUG11:16:00:00"dt then group=49;

   else if "18AUG11:17:00:00"dt <= trade_date_time <= "19AUG11:16:00:00"dt then group=50;

   else if "19AUG11:17:00:00"dt <= trade_date_time <= "20AUG11:00:00:00"dt then group=51;

   else if "21AUG11:17:00:00"dt <= trade_date_time <= "22AUG11:16:00:00"dt then group=52;

   else if "22AUG11:17:00:00"dt <= trade_date_time <= "23AUG11:16:00:00"dt then group=53;

   else if "23AUG11:17:00:00"dt <= trade_date_time <= "24AUG11:16:00:00"dt then group=54;

   else if "24AUG11:17:00:00"dt <= trade_date_time <= "25AUG11:16:00:00"dt then group=55;

   else if "25AUG11:17:00:00"dt <= trade_date_time <= "26AUG11:16:00:00"dt then group=56;

   else if "26AUG11:17:00:00"dt <= trade_date_time <= "27AUG11:00:00:00"dt then group=57;

   else if "28AUG11:17:00:00"dt <= trade_date_time <= "29AUG11:16:00:00"dt then group=58;

   else if "29AUG11:17:00:00"dt <= trade_date_time <= "30AUG11:16:00:00"dt then group=59;

   else if "30AUG11:17:00:00"dt <= trade_date_time <= "31AUG11:16:00:00"dt then group=60;

   else if "31AUG11:17:00:00"dt <= trade_date_time <= "01SEP11:16:00:00"dt then group=61;

   else if "01SEP11:17:00:00"dt <= trade_date_time <= "02SEP11:16:00:00"dt then group=62;

   else if "02SEP11:17:00:00"dt <= trade_date_time <= "03SEP11:00:00:00"dt then group=63;

   else if "06SEP11:00:00:00"dt <= trade_date_time <= "06SEP11:16:00:00"dt then group=64;

   else if "06SEP11:17:00:00"dt <= trade_date_time <= "07SEP11:16:00:00"dt then group=65;

   else if "07SEP11:17:00:00"dt <= trade_date_time <= "08SEP11:16:00:00"dt then group=66;

   else if "08SEP11:17:00:00"dt <= trade_date_time <= "09SEP11:16:00:00"dt then group=67;

   else if "09SEP11:17:00:00"dt <= trade_date_time <= "10SEP11:00:00:00"dt then group=68;

   else if "11SEP11:17:00:00"dt <= trade_date_time <= "12SEP11:16:00:00"dt then group=69;

   else if "12SEP11:17:00:00"dt <= trade_date_time <= "13SEP11:16:00:00"dt then group=70;

   else if "13SEP11:17:00:00"dt <= trade_date_time <= "14SEP11:16:00:00"dt then group=71;

   else if "14SEP11:17:00:00"dt <= trade_date_time <= "15SEP11:16:00:00"dt then group=72;

   else if "15SEP11:17:00:00"dt <= trade_date_time <= "16SEP11:16:00:00"dt then group=73;

   else if "16SEP11:17:00:00"dt <= trade_date_time <= "17SEP11:00:00:00"dt then group=74;

   else if "18SEP11:17:00:00"dt <= trade_date_time <= "19SEP11:16:00:00"dt then group=75;

   else if "19SEP11:17:00:00"dt <= trade_date_time <= "20SEP11:16:00:00"dt then group=76;

   else if "20SEP11:17:00:00"dt <= trade_date_time <= "21SEP11:16:00:00"dt then group=77;

   else if "21SEP11:17:00:00"dt <= trade_date_time <= "22SEP11:16:00:00"dt then group=78;

   else group=0;

run;


Accepted Solutions
Solution
‎05-01-2012 04:06 PM
Super User
Posts: 17,819

Re: reducing the size of my code

something like the following should work.

It can be simplified even further but this gets you started and I'm sure someone will come and produce a better solution.

I'm also not sure how to compare time at the top of my head, so the '16:00:00't might not be correct either.

data want;

     set have;

     date_start='22Jun2011'd;

     date_end='22Sep2011'd;

     time_trade=time(trade_date_time);

      if time_trade>'16:00:00't then date_temp=datepart(trade_date_time)+1;

     else date_temp=datepart(trade_date_time);

     if date_temp<date_start then group=1;

     else if date<date_end then group=date_temp-date_start;

     else group=0;

    

run;

View solution in original post


All Replies
Solution
‎05-01-2012 04:06 PM
Super User
Posts: 17,819

Re: reducing the size of my code

something like the following should work.

It can be simplified even further but this gets you started and I'm sure someone will come and produce a better solution.

I'm also not sure how to compare time at the top of my head, so the '16:00:00't might not be correct either.

data want;

     set have;

     date_start='22Jun2011'd;

     date_end='22Sep2011'd;

     time_trade=time(trade_date_time);

      if time_trade>'16:00:00't then date_temp=datepart(trade_date_time)+1;

     else date_temp=datepart(trade_date_time);

     if date_temp<date_start then group=1;

     else if date<date_end then group=date_temp-date_start;

     else group=0;

    

run;

Contributor
Posts: 56

Re: reducing the size of my code

thanks, im just trying this code, but im not sure it will do friday probably, notice some of the times finish as 00:00:00

Contributor
Posts: 56

Re: reducing the size of my code

that suggested code gives me 90 groups, but the original code only gives 76 groups!

Super User
Posts: 17,819

Re: reducing the size of my code

Well I don't have data to test it you do...it gets you closer though. Reading 76 lines of code its easy to miss things.

I'm guessing only its only weekdays that are considered, thats why from group4 to group5 your missing the 25th? Then you can consider using the intck function instead of straight subtraction with the 'weekday' setting.

intck('weekday', date_temp, date_start);

You can also add the logic for Fridays by using another if statement and a weekday function, weekday(datepart(your_date))=6 is Friday I believe.

Super User
Posts: 9,676

Re: reducing the size of my code

I would consider using proc format cntlin=  ;

Super User
Posts: 17,819

Re: reducing the size of my code

You'd still have to type it all out at least once for this type of data so the 'length' of your code isn't significantly different is it?

Super User
Posts: 9,676

Re: reducing the size of my code

Reeza,

We can use Data step to get such dataset ,if there are some rules . Smiley Happy

What is your opinion ?

Did you attend the SGF 2012 ?

FriedEgg saw you.

Ksharp

Super User
Posts: 17,819

Re: reducing the size of my code

True...the code would go somewhere though so it depends on what the eventual process is.

I was at SGF 2012, not presenting this year though, were you?

I said hi to FriedEgg and looked for him at the Kickback party but didn't see him.

PROC Star
Posts: 7,363

Re: reducing the size of my code

@Fareeza: You didn't see him because I had him cornered in a discussion outside in the smoking section (even though he doesn't smoke)!  Since I'm a pipe smoker I usually end up outside and, honestly, I learn more fromthe discussions I get into out there than anywhere else at the conference.  Besides, it was a lot quieter out there.

Super Contributor
Posts: 1,636

Re: reducing the size of my code

Are you a smoker???

PROC Star
Posts: 7,363

Re: reducing the size of my code

@ Linlin: Yes!  Unfortunately when I was teaching Psychology at a university (a long time ago), profs were almost expected to be pipe smokers and could smoke in class.  Long story short: I got addicted and, while I've tried, am stuck with that fact.  However, I'll never smoke around Fareeza or anyone else who is bothered by it.

Super Contributor
Posts: 1,636

Re: reducing the size of my code

Now I know what is in your hand in the picture. I am allergic to perfume, every product I use has to be fragrance free.

PROC Star
Posts: 7,363

Re: reducing the size of my code

I NEVER wear perfume!  BTW, I'm trying to entice the BASUG to invite me to present at one of your meetings.  Apparently, I have to change my first name to "Mike".  You would have to have followed the discussion on the SGF2012 LinkedIn page for that to make sense.

Super Contributor
Posts: 1,636

Re: reducing the size of my code

That would be great if you come! At last BASUG meeting I asked Mike to sign my handout.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 18 replies
  • 569 views
  • 6 likes
  • 6 in conversation