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

i want to assign values for EXD based on the below condition

EXT is "pine", EXD should be assigned by value 2 for the first 2 days, value 4 for the next 2 days and value 6 for the remaining Period A. If EXT is "ozi", EXD should be assigned by value 5 for the first 2 days, value 10 for the next 2 days and value 15 for the remaining Period A.

 

Attached test data. can any one suggest how to do it.?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @vraj1,

 

There are at least two possible interpretations of your criteria and hence different solutions:

 

  1. The "first 2 days" are days 1 and 2 (according to variable DAY), the "next 2 days" are days 3 and 4 and so on.
    data want;
    set test(drop=exd);
    if period='A' then do;
           if ext='pine' then exd=2*min(int((day+1)/2),3);
      else if ext='ozi'  then exd=5*min(int((day+1)/2),3);
    end;
    run;
  2. The "first 2 days" with EXT='pine' correspond to the first two observations with PERIOD='A' and EXT='pine' within an ID regardless of DAY (but assuming chronological sort order), etc.
    data want;
    set test(drop=exd);
    by id day;
    if first.id then call missing(_cp, _co);
    if period='A' then do;
           if ext='pine' then do; _cp+1; exd=2*min(int((_cp+1)/2),3); end;
      else if ext='ozi'  then do; _co+1; exd=5*min(int((_co+1)/2),3); end;
    end;                                     
    drop _:;
    run;

View solution in original post

1 REPLY 1
FreelanceReinh
Jade | Level 19

Hi @vraj1,

 

There are at least two possible interpretations of your criteria and hence different solutions:

 

  1. The "first 2 days" are days 1 and 2 (according to variable DAY), the "next 2 days" are days 3 and 4 and so on.
    data want;
    set test(drop=exd);
    if period='A' then do;
           if ext='pine' then exd=2*min(int((day+1)/2),3);
      else if ext='ozi'  then exd=5*min(int((day+1)/2),3);
    end;
    run;
  2. The "first 2 days" with EXT='pine' correspond to the first two observations with PERIOD='A' and EXT='pine' within an ID regardless of DAY (but assuming chronological sort order), etc.
    data want;
    set test(drop=exd);
    by id day;
    if first.id then call missing(_cp, _co);
    if period='A' then do;
           if ext='pine' then do; _cp+1; exd=2*min(int((_cp+1)/2),3); end;
      else if ext='ozi'  then do; _co+1; exd=5*min(int((_co+1)/2),3); end;
    end;                                     
    drop _:;
    run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 847 views
  • 0 likes
  • 2 in conversation