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

No, just ignore that option. Not needed. Will work without it

 

/*Added additional variables to the sample HAVE*/
data have;
input id   day  drinks ;
blah=rand('integer',1,100);
blahblah=rand('integer',1,100);
blahblahblah=rand('integer',1,100);
cards;
1     2      1
1     3      3
1     5      4
2     3      1
3     2      1
3     4      2
;

data want;
if _n_=1 then do;
if 0 then set have;
dcl hash H (dataset:'have') ;
   h.definekey  ("id",'day') ;
   h.definedata ("drinks") ;
   h.definedone () ;
end;
set have(keep=id blah blahblah blahblahblah);
by id;/*assuming it's sorted by id as your sample suggests*/
if first.id;
do day=1 to 5;
rc=h.find();
if rc ne 0 then drinks=0;
output;
end;
drop rc;
run;
mkt_apprentice
Obsidian | Level 7

A BIG THANKS TO YOU!!! Smiley Happy 

novinosrin
Tourmaline | Level 20

You are welcome! have a good one!

andreas_lds
Jade | Level 19

Thanks @novinosrin for providing data in usable form.

 

Another way to solve the issue:

data want;
    set have;
    by id;
    
    length expectedDay backupDay backupDrinks 8;
    retain expectedDay;
    drop expectedDay backupDay backupDrinks;
    
    if first.id then do;
        expectedDay = 1;
    end;
    
    if day ^= expectedDay then do;
        backupDay = day;
        backupDrinks = drinks;
        drinks = 0;
        
        do day = expectedDay to backupDay-1;
            output;
        end;
        
        drinks = backupDrinks;
    end;
    
    output;
    
    expectedDay = day + 1;
    
    if last.id and day ^= 5 then do;
        drinks = 0;
        do day = day+1 to 5;
            output;
        end;
    end;
    
run;

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 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
  • 18 replies
  • 1181 views
  • 4 likes
  • 3 in conversation