BookmarkSubscribeRSS Feed
thomask23
Fluorite | Level 6

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?



5 REPLIES 5
Steelers_In_DC
Barite | Level 11

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;

ballardw
Super User

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.

thomask23
Fluorite | Level 6

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

Steelers_In_DC
Barite | Level 11

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.

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1859 views
  • 0 likes
  • 4 in conversation