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

Try this new modified version of the code, I have added a different city, so you can see how the code would behave with different time intervals, and you should be able to take it from here.....

 

data have;
    length    state $2
        city $10        
        fmtname $7
        start 8
        label $5
        hour 4
        minute 4;
    input state city fmtname start :date9. label hour minute;
    format start date9. label $5.;
    datalines;
MD Baltimore sunrise 17Jan2017 07:24 7 24
MD Baltimore sunset 17Jan2017 17:10 17 10
DE Dover sunrise 13JUL2017 04:47 4 47
DE Dover sunset 13JUL2017 19:28 19 28
;
run;

data want(drop=i);
    set have;
    array hours {24} 4 hour_0-hour_23;
    format hour_: BEST8.4;
    
    /* Set all Hours to 0 */
    do i=1 to dim(hours);
        hours[i]=1;
    end;

    if (fmtname='sunrise') then
        /* Assign the Subsequent Hours the factors (minutes/60) */
        do i=(hour+2) to (12+hour-1);
            hours[i]=minute/60;
        end;
    else if (fmtname='sunset') then
    do;
        /* Assign the Hours prior 8:00 am the factors (minutes/60) */
        do i=8 to 1 by -1;
            hours[i]=minute/60;
        end;
        /* Assign the Subsequent Hours the factors (minutes/60) */
        do i=(hour+2) to dim(hours);
            hours[i]=minute/60;
        end;

    end;
    output;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 22108 views
  • 1 like
  • 5 in conversation