New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
techsassy
Obsidian | Level 7

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!!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

1 REPLY 1
ballardw
Super User

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 2770 views
  • 0 likes
  • 2 in conversation