DATA Step, Macro, Functions and more

Set two dataset with if condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Set two dataset with if condition

An example program is followed. i am wondering why the last observation in dataset C had a value of 'B' in the column OUTCOME?

Thank you all!!

 

 

DATA A;

INPUT OBS $ OUTCOME $;
CARDS;
1 A
2 B
;
RUN;


DATA B;
INPUT OBS $ RESULT $;
CARDS;
1 1
2 2
3 .
;
RUN;
DATA C;
SET A(IN = A) B(IN = B);
IF A THEN DATASET = 'A';
IF B THEN DO;
DATASET = 'B';
IF RESULT = '1' THEN OUTCOME = 'A';
ELSE IF RESULT = '2' THEN OUTCOME = 'B';
END;
RUN;

 


Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 9,868

Re: Set two dataset with if condition

Posted in reply to TristanTang

First, keep in mind that all variables in datasets named in a set (or merge) statement are automatically retained.

So, when the second dataset B is being read, outcome will always be retained from the previous iteration of the data step.

Since the next-to-last observation in B has a result of '2', outcome is set to 'B'; In the last observation, result is missing, so none of the conditons in the do/end block is met, and outcome stays at value 'B'.

 

BTW stop coding in uppercase-only. Interpreters and compilers understand lowercase since the 60's, and it makes code easier to read.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
4 weeks ago
Super User
Posts: 9,868

Re: Set two dataset with if condition

Posted in reply to TristanTang

First, keep in mind that all variables in datasets named in a set (or merge) statement are automatically retained.

So, when the second dataset B is being read, outcome will always be retained from the previous iteration of the data step.

Since the next-to-last observation in B has a result of '2', outcome is set to 'B'; In the last observation, result is missing, so none of the conditons in the do/end block is met, and outcome stays at value 'B'.

 

BTW stop coding in uppercase-only. Interpreters and compilers understand lowercase since the 60's, and it makes code easier to read.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: Set two dataset with if condition

Posted in reply to KurtBremser

Thanks for the answer.

For me, uppercase is easier to read. Anyway, thank you for your suggestion!!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 97 views
  • 0 likes
  • 2 in conversation