I need to add additional observations Balamt=0 where a the previous month (Monyear) value is null for a cateogy,
Would the best approach be to create new table(see below) and then do a Union query?
data test;
input monyear $ val $ Category $;
datalines;
mar-15 0 1 mkt1
mar-15 0 2 mkt2
mar-15 0 1 mkt7
;
or could i create a if then data step to add the Balamt -0 if the previous months data is null?
Here you go, hope this helps:
data have;
infile datalines dsd;
informat monyear2 date9.;
format monyear2 date9.;
input monyear $ monyear2 val $ Category $;
datalines;
,31MAR2015,0 1,mkt1
mar-15,,0 2,mkt2
mar-15,,0 1,mkt7
;
run;
data want;
set have;
/*if using characters*/
if monyear = '' then balamt = '0';
/*if using numbers*/
if monyear2 = . then balamt2 = 1;
run;
It will help if you can show some example data from the base data set and what the desired output for the example data would be.
below is an example of the current dataset and the desired data
current table
BALAMT MONYEAR Segment
100 Mar-15 agncy7
200 Mar-15 agncy2
300 Mar-15 agncy3
400 Mar-15 agncy1
100 Mar-15 sports4
needed table
BALAMT MONYEAR Segment
0 Feb-14 agncy1
400 Mar-15 agncy1
0 Feb-14 agncy2
200 Mar-15 agncy2
0 Feb-14 agncy3
300 Mar-15 agncy3
0 Feb-14 agncy7
100 Mar-15 agncy7
0 Feb-14 sports4
100 Mar-15 sports4
I see what you meant about the union now, I misunderstood what you were asking for. Yes I would build a data set with the date range first and then join. I've used proc expand for this as well.
If this is really an exact representation of the input and output, you could try:
data want;
set have;
output;
balamt=0;
monyear='Feb-14';
output;
run;
Sorting might be necessary afterwards. Good luck.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.