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

Hello all,

 

I have a dataset that looks like this:

 

DATE1         DATE2         

31/03/2018  31/03/2018

30/04/2018  31/03/2019

31/05/2018  31/03/2020 

 

I need to create a new variable, TYPE, that will have the same value for all rows. I need a program that evaluates the first values of DATE1 and DATE2: 

  - If, for the first row, DATE1=DATE2 then this variable will always take the value 'INI';

  - If, for the first row, DATE1 not eq DATE2, then this variable will always take the value 'EXR'. 

In my example, the data with the new variable would look like this:

 

DATE1         DATE2         TYPE

31/03/2018  31/03/2018   INI

30/04/2018  31/03/2019   INI

31/05/2018  31/03/2020   INI

 

I have tried conditional expressions with case when in proc sql but I am only able to obtain an expression that evaluates all rows one by one, thus not generating the same value for all rows.

 

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

Use a data step with a retain statement :

data want;
    set have;
    length TYPE $3.;
    retain TYPE;
    if _N_=1 then TYPE=ifc(DATE1=DATE2,'INI','EXR');
run;    

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

Use a data step with a retain statement :

data want;
    set have;
    length TYPE $3.;
    retain TYPE;
    if _N_=1 then TYPE=ifc(DATE1=DATE2,'INI','EXR');
run;    
mariapf
Fluorite | Level 6

Thank you so much! This code has worked for me

andreas_lds
Jade | Level 19

You need to retain the once evaluated value of "Type":

data have;
   informat Date1 Date2 ddmmyy10.;
   format Date1 Date2 ddmmyys10.;
   input Date1 Date2;
   datalines;
31/03/2018 31/03/2018
30/04/2018 31/03/2019
31/05/2018 31/03/2020 
;
run;

data want;
   set have;
   length Type $ 3;
   retain Type;
   
   if _n_ = 1 then do;
      Type = ifc(Date1 = Date2, 'INI', 'EXR');
   end;
run;
   
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
  • 3 replies
  • 1796 views
  • 1 like
  • 3 in conversation