SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bk8
Calcite | Level 5 bk8
Calcite | Level 5

Hi experts,

I have two datasets say, first and second. They can generate any values every month. The values of same column needs to be add and substract in first dataset (as shown in Result below). The result  (a_out and b_out) needs to be compared with value from second dataset (c_out).

Compare c_out with a_out
Also compare c_out with b_out

Display 'matching' or 'not matching'.

The validation needs to be sent to the users. 


data first;
input a b;
datalines;
22 25
44 0
33 14
22 0
;
run;

Result:
a_out = (22 + 44) - (33 + 22)
b_out = ( 25 + 0) - (14 + 0)

I have c_out from other table.

data second;
input c_out;
datalines;
11
;
run;

 

 

The same calculation needed for several LOBs and I want to write macro for the same. Please reply with the above solution at least. Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's a way to get the FINAL data set.  But leaving a message for the user is far too broad a task to attempt without more clarification on the form of the message.  I leave that part to you.

data second;
input c_out;
datalines;
11
;

data final;
input a b;
retain factor 1;
if _n_=3 then factor = -1;
a_out + factor * a;
b_out + factor * b;
if _n_=4;
set second;
keep a_out b_out c_out;
datalines;
22 25
44 0
33 14
22 0
;

View solution in original post

4 REPLIES 4
ballardw
Super User

@bk8 wrote:

Hi experts,

I have two datasets say, first and second. They can generate any values every month. The values of same column needs to be add and substract in first dataset (as shown in Result below). The result  (a_out and b_out) needs to be compared with value from second dataset (c_out).

Compare c_out with a_out
Also compare c_out with b_out

Display 'matching' or 'not matching'.

The validation needs to be sent to the users. 


data first;
input a b;
datalines;
22 25
44 0
33 14
22 0
;
run;

Result:
a_out = (22 + 44) - (33 + 22)
b_out = ( 25 + 0) - (14 + 0)

I have c_out from other table.

data second;
input c_out;
datalines;
11
;
run;

 

 

The same calculation needed for several LOBs and I want to write macro for the same. Please reply with the above solution at least. Thank you in advance.


Is data first always exactly four rows of data? If not, you need to provide a rule based on the contents of the data set that will allow us to determine which rows get added and which subtracted.

 

You should really provide what the output should look like after the compare. I can interpret that a number of ways.

bk8
Calcite | Level 5 bk8
Calcite | Level 5
Yes, the first dataset always has 4 rows and second dataset has 1 row.
For the above example, I need new dataset to be created, say 'final' and the output should look like

a_out b_out c_out
11 11 11

And the message should be like,
'c_out is matching with a_out and b_out'.

If the results are different then we can give error message as for e.g., 'c_out is matching with b_out but is off by $3 with a_out' (its just sample).
Please let me know if it is not clear yet. Thank you in advance.
Astounding
PROC Star

Here's a way to get the FINAL data set.  But leaving a message for the user is far too broad a task to attempt without more clarification on the form of the message.  I leave that part to you.

data second;
input c_out;
datalines;
11
;

data final;
input a b;
retain factor 1;
if _n_=3 then factor = -1;
a_out + factor * a;
b_out + factor * b;
if _n_=4;
set second;
keep a_out b_out c_out;
datalines;
22 25
44 0
33 14
22 0
;
bk8
Calcite | Level 5 bk8
Calcite | Level 5
Thank you so much @Astounding. This logic works well and able to tweek my code and got the result.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 4 replies
  • 885 views
  • 1 like
  • 3 in conversation