Hi SAS Community,
I want to create a set of features based on a date index- that is, a set of features based on a date on one line per ID key.
This is the data:
What I want it to look like:
This is the code I came up with:
data x;
set x;
if date_field = '30SEP2018:00:00:00.000'dt then SEP2018_BAL= amount;
else if date_field = '31OCT2018:00:00:00.000'dt then OCT2018_BAL= amount;
else if date_field = '30NOV2018:00:00:00.000'dt then NOV2018_BAL= amount;
else if date_field = '31DEC2018:00:00:00.000'dt then DEC2018_BAL= amount;
else if date_field = '31JAN2019:00:00:00.000'dt then JAN2019_BAL= amount;
else if date_field = '28FEB2019:00:00:00.000'dt then FEB2019_BAL= amount;
run;
And this is the output I am getting, which is not ideal:
Let me know if you need any more info from me. Thank you!
Hi @sassing and welcome to the SAS Community 🙂
Here's how I would do it
data have;
retain ID_key "a1_1234";
input amount date_field :anydtdtm22.;
format date_field datetime22.;
datalines;
710937.5 30SEP2018:00:00:00.000
713653.38 31OCT2018:00:00:00.000
705974.23 30NOV2018:00:00:00.000
704847.1 31DEC2018:00:00:00.000
704016.17 31JAN2019:00:00:00.000
703178.11 28FEB2019:00:00:00.000
;
data temp;
set have;
vn=cats(put(datepart(date_field), monyy7.), '_BAL');
run;
proc transpose data=temp out=want(drop=_:);
by ID_key;
id vn;
var amount;
run;
Hi @sassing and welcome to the SAS Community 🙂
Here's how I would do it
data have;
retain ID_key "a1_1234";
input amount date_field :anydtdtm22.;
format date_field datetime22.;
datalines;
710937.5 30SEP2018:00:00:00.000
713653.38 31OCT2018:00:00:00.000
705974.23 30NOV2018:00:00:00.000
704847.1 31DEC2018:00:00:00.000
704016.17 31JAN2019:00:00:00.000
703178.11 28FEB2019:00:00:00.000
;
data temp;
set have;
vn=cats(put(datepart(date_field), monyy7.), '_BAL');
run;
proc transpose data=temp out=want(drop=_:);
by ID_key;
id vn;
var amount;
run;
Anytime 🙂 You too
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.