Hello guys,
I have below data set in which i want the values of reason in one record by subject & Visit description.
Subject | Visit_description | Start_date | End_date | Reason | Other_Reason |
1 | Assessment 1 | 6/26/2020 | 6/26/2020 | CONVULSIONS | |
1 | Assessment 1 | 6/26/2020 | 6/26/2020 | DEHYDRATION | |
1 | Assessment 1 | 6/26/2020 | 6/26/2020 | FEEDING DIFFICULTY | |
1 | Assessment 1 | 6/26/2020 | 6/26/2020 | OTHER | iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii |
1 | Assessment 2 | 6/27/2020 | 6/27/2020 | IRREGULAR OR PERIODIC RESPIRATION | |
1 | Assessment 2 | 6/27/2020 | 6/27/2020 | IRRITABILITY OR AGITATION | |
1
|
Assessment 2 | 6/27/2020 | 6/27/2020 | LETHARGY OR EXCESSIVE SLEEPINESS |
Output should be like below table
Subject | Visit_description | Start_date | End_date | Reason | Other_Reason |
1 | Assessment 1 | 6/26/2020 | 6/26/2020 | CONVULSIONS,DEHYDRATION,FEEDING DIFFICULTY,OTHER | iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii |
1 | Assessment 2 | 6/27/2020 | 6/27/2020 | IRREGULAR OR PERIODIC RESPIRATION,IRRITABILITY OR AGITATION,LETHARGY OR EXCESSIVE SLEEPINESS |
Try next code:
data want;
set have(rename=(reason = _reason));
by Sunject Visit_desription;
retain reason;
length reason $100; /* adapt to max neaded */
if first.Visit_desription then reason = ' ';
reason = catx(',',reason,_reason);
if last.Visit_desription then output;
run;
@Soham0707 wrote:
not working 😞
Very bad answer, totally unhelpful.
Please describe in detail what "did not work".
Did you get ERRORs or WARNINGs? Or did the code run, but not produce the expected output?
In all cases, post the log, using the </> button. Point out the difference between expected and actual result.
By "post the log" I meant to post the whole log of your step, including the complete code of the step and all follow-up messages. And, as requested, you must (MUST) use the </> button to keep the formatting of the log. This is important, as the horizontal position of ERROR markers helps in identifying the cause of an ERROR, and this positioning is destroyed by the main posting window.
@Soham0707 wrote:
Sorry for that..
This warning i got and output is not matching..
WARNING: In a call to the CATX function, the buffer allocated for the result was not long enough to contain the concatenation of all the arguments. The correct result would contain 5
characters, but the actual result might either be truncated to 1 character(s) or be completely blank, depending on the calling environment. The following note indicates the
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
left-most argument that caused truncation.
NOTE: Argument 3 to function CATX(',',' '[12 of 50 characters shown],'OTHER '[12 of 160 characters shown]) at line 30431 column 14 is invalid.
If you check the posted code it written explicitley:
length reason $100; /* adapt to max needed */
I cannot know what is the maximum number of reasons will be concatenated.
You can change the length to 1000 up to 32000 but better evaluate it or just increase until there will be no more such warnings.
Do you need or prefer sorting the concatenated reasons or leave them in order as they arrived? Can there be a reason more then once per subject?
Providing example data in usable form (self-contained data step) is also a MUST if you want quick answers with a valid solution.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.