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
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;
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;
Thank you so much! This code has worked for me
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;
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.
Ready to level-up your skills? Choose your own adventure.