Hi All,
I want to create new column 'new_visit' where all visit information of unique patient available with repetilike below
pid | age | visit | d1 | new_visit |
101 | 21 | 1 | 1 | 1, 2 |
101 | 21 | 2 | 1 | 1, 2 |
101 | 21 | 1 | 4 | 1, 2 |
102 | 34 | 2 | 1 | 2 |
103 | 31 | 1 | 1 | 1 |
104 | 25 | 1 | 2 | 1, 2,3 |
104 | 25 | 2 | 2 | 1, 2,3 |
104 | 25 | 3 | 2 | 1, 2,3 |
104 | 25 | 1 | 6 | 1, 2,3 |
104 | 25 | 2 | 5 | 1, 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;
"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.
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.
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;
"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.
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.
That code ran fine for me.
Define what you mean by not working.
Show us the log for the data step, the proc format and the proc sql. Include the code and all the messages.
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".
Kind regards,
Amir.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.