- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I have some student course records for different courses taken, e.g. MATH 201, 220, 255. Each course is associated with a student ID and appears as an individual record. Using the retain statement, I'd like to iteratively concatenate each course number so that I end up with a final record that has all courses a student has taken in a single string, e.g. selection = "201-220-255".
My recollection is that I've done this many, many times using double vertical bars/pipes, but it doesn't seem to want to work today. This is the code that I would normally use:
data output_data; length selection $ 50.; set input_data; by student_id; if first.student_id then selection = ' '; selection = selection||'-'||course; retain selection; run;
But today it seems this code only works if I remove the bars/pipes and instead use CATS, as follows:
data output_data; length selection $ 50.; set input_data; by student_id; if first.student_id then selection = ' '; selection = cats(selection,'-',course); retain selection; run;
Either I'm completely wrong that the former method has worked in the past, or something about my environment has changed. Our ITS department just gave us new computers with fresh SAS installs - could this be what's at play, or did I misremember successfully using double bars/pipes in the past?
Thank you!!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try something like:
data output_data; length selection $ 50.; set input_data; by student_id; if first.student_id then selection = ' '; selection = strip (selection) ||'-'||course; retain selection; run;
The || operator retains the entire length of the defined variable for operations, including trailing blanks unless explicitly instructed not to.
So using
selection = selection||'-'||course;
the previously defined value of selection occupies all of the character spaces and has not place to add the other characters.
The CATX and related functions CATT and CATS remove spaces for any of the arguments.
data example; length var var2$ 25.; var=' '; var3= 'abc'; var= var||var3; var2 = strip(var)||var3; run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try something like:
data output_data; length selection $ 50.; set input_data; by student_id; if first.student_id then selection = ' '; selection = strip (selection) ||'-'||course; retain selection; run;
The || operator retains the entire length of the defined variable for operations, including trailing blanks unless explicitly instructed not to.
So using
selection = selection||'-'||course;
the previously defined value of selection occupies all of the character spaces and has not place to add the other characters.
The CATX and related functions CATT and CATS remove spaces for any of the arguments.
data example; length var var2$ 25.; var=' '; var3= 'abc'; var= var||var3; var2 = strip(var)||var3; run;