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

Hi All,

I want to create new column 'new_visit' where all visit information of unique patient available with repetilike below

pidagevisitd1new_visit
10121111, 2
10121211, 2
10121141, 2
10234212
10331111
10425121, 2,3
10425221, 2,3
10425321, 2,3
10425161, 2,3
10425251, 2,3

 

data test;
input pid age visit d1 ;
cards;
101 21 1 1
101 21 2 1
101 21 1 4 
102 34 2 1 
103 31 1 1 
104 25 1 2
104 25 2 2
104 25 3 2
104 25 1 5
104 25 2 5
 ;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

"Not working" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "<>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "<>" icon or attached as text to show exactly what you have and that we can test code against.

View solution in original post

8 REPLIES 8
Amir
PROC Star

Hi,

 

How about the following?

 

data have;
	input pid age visit d1 ;
	cards;
101 21 1 1
101 21 2 1
101 21 1 4 
102 34 2 1 
103 31 1 1 
104 25 1 2
104 25 2 2
104 25 3 2
104 25 1 5
104 25 2 5
;


data want;
	length new_visit $ 10;
	
	do until(last.pid);
		set have;
		by pid;
		
		new_visit = catx(',',new_visit,ifc(find(new_visit,cats(visit)),'',cats(visit)));
	end;
	
	do until(last.pid);
		set have;
		by pid;
		
		output;
	end;
run;

 

 

Kind regards,

Amir.

 

abraham1
Obsidian | Level 7

Thank you Amir, It works perfectly. 

In that code, when I format the code using proc sql, its not working. Can you guide what is the mistake I am doing as I am new.

 

proc format;
value abc 1='not present'
      2='Present'
      3='Available'
      ;
run;

proc sql;
	create table aa as
	select pid, age, visit format=abc., d1 
	from test;
quit;

ballardw
Super User

"Not working" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "<>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "<>" icon or attached as text to show exactly what you have and that we can test code against.

abraham1
Obsidian | Level 7
data test;
input pid age visit d1 ;
cards;
101 21 1 1
101 21 2 1
101 21 1 4 
102 34 2 1 
103 31 1 1 
104 25 1 2
104 25 2 2
104 25 3 2
104 25 1 5
104 25 2 5
 ;
run;

proc format;
value abc 1='not present'
      2='Present'
      3='Available'
      ;
run;

proc sql;
	create table aa as
	select pid, age, visit format=abc., d1 
	from test;
quit;
	

I am not getting any error or warning in log but still it is not working.

Tom
Super User Tom
Super User

That code ran fine for me. 

Define what you mean by not working.

Hemamalini
Calcite | Level 5
I don't know why the exact code is not formating the visit value in output dataset in Unix env. I can do the same with case statement which is working but not with this above aproach.
ballardw
Super User

Show us the log for the data step, the proc format and the proc sql. Include the code and all the messages.

 

 

Amir
PROC Star

Hi,

 

When I run the code, the table aa has been created as per the below screenshot, please show what your aa table looks like and point out what value is wrong and what you think it should be and why.

 

Lastly, are you sure you are looking at the table "aa" and not the table "test".

 

Amir_0-1647594279698.png

 

 

 

Kind regards,

Amir.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 816 views
  • 1 like
  • 5 in conversation