BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chetan3125
Obsidian | Level 7

I am trying to merge and create new records. 

However with following code, the records for new2 are not output.

I want records for new1 and new2 in the final data based on the conditions. 

Any solutions ? or may be sql alternative ?
Thanks. 

data d1 ;
input VISIT  PARAMCD $3-10 PATHOGEN $11-13 subject $14-16;
datalines ;
10 param3 P1 1
40 param1 P2 1
40 param3 P2 1
40 param2 P2 1
10 param1 P2 2
10 param3 P2 2
10 param2 P2 2
40 param1 P3 3
40 param3 P3 3
40 param2 P3 3
;
run;

Data param1 param2 param3 ;
set d1 ;
		 if paramcd='param1' then output  param1 ;
		 if paramcd='param2' then output param2 ;
		 if paramcd='param3' then output param3 ;
drop paramcd ;
run ;

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
		if paramcd='param1' and param2 then do ; paramcd= 'new1' ; output ;end; 
		if paramcd='param1' and param3 then do ; paramcd= 'new2' ; output ;end; 
Run;




1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Please look at the result from this code:

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
		if paramcd='param1' and param2 then do ; paramcdnew= 'new1' ; output ;end; 
		if paramcd='param1' and param3 then do ; paramcdnew= 'new2' ; output ;end; 
Run;

At least with your example data, if I understand what you are attempting, when you set Paramcd to 'new1' the value is no longer there for use in the second IF.

Try this: Test the Paramcd value only one time.

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
	if paramcd='param1' Then do;
           if param2 then do ; paramcd= 'new1' ; output ;end; 
	   if param3 then do ; paramcd= 'new2' ; output ;end; 
       end;
Run;

View solution in original post

3 REPLIES 3
CarmineVerrell
SAS Employee

Seems you have an issue with the logic and you dropped the column paramcd, so i have modified code and included below.

 

data d1 ;
input VISIT PARAMCD $3-10 PATHOGEN $11-13 subject $14-16;
datalines ;
10 param3 P1 1
40 param1 P2 1
40 param3 P2 1
40 param2 P2 1
10 param1 P2 2
10 param3 P2 2
10 param2 P2 2
40 param1 P3 3
40 param3 P3 3
40 param2 P3 3
;
run;

Data param1 param2 param3 ;
set d1 ;
if paramcd='param1' then output param1 ;
if paramcd='param2' then output param2 ;
if paramcd='param3' then output param3 ;

run ;

data d2 ;
merge d1 (in=a where= (visit ne . ))
param1 (in=param1 )
param2 (in=param2 )
param3 (in=param3 ) ;
by subject visit pathogen ;
if paramcd='param1' and param1 then do ; paramcd= 'new1' ; output ;end;
if paramcd='param2' and param1 then do ; paramcd= 'new2' ; output ;end;
Run;

ballardw
Super User

Please look at the result from this code:

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
		if paramcd='param1' and param2 then do ; paramcdnew= 'new1' ; output ;end; 
		if paramcd='param1' and param3 then do ; paramcdnew= 'new2' ; output ;end; 
Run;

At least with your example data, if I understand what you are attempting, when you set Paramcd to 'new1' the value is no longer there for use in the second IF.

Try this: Test the Paramcd value only one time.

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
	if paramcd='param1' Then do;
           if param2 then do ; paramcd= 'new1' ; output ;end; 
	   if param3 then do ; paramcd= 'new2' ; output ;end; 
       end;
Run;

chetan3125
Obsidian | Level 7

@ballardw This worked. Thanks. Also changed the output var name.

 

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 251 views
  • 0 likes
  • 3 in conversation