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

Hi! I'm trying to automate a piece of code that works well.

 

My goal is to list all the students who did an activity, but I need to report this on a list within my current table. So, I use the following code:

 

 

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_45);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

But I don't want to have to indicate the number of students manually, as this has caused me to generate many errors. So I'm trying to do it with the following code, but it's not working. Can someone help me?

 

Attempt using &n_students:

 

PROC SQL noprint;
	SELECT  count(*) 
	INTO :n_students
	FROM WORK.class_activity;
	QUIT;

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_&n_students);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
students=catx(sp, of STUDENT_:);

Depends on your naming structure to some degree. This blog post covers some of the options and how they work:

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 


@Neeydchi wrote:

Hi! I'm trying to automate a piece of code that works well.

 

My goal is to list all the students who did an activity, but I need to report this on a list within my current table. So, I use the following code:

 

 

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_45);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

But I don't want to have to indicate the number of students manually, as this has caused me to generate many errors. So I'm trying to do it with the following code, but it's not working. Can someone help me?

 

Attempt using &n_students:

 

PROC SQL noprint;
	SELECT  count(*) 
	INTO :n_students
	FROM WORK.class_activity;
	QUIT;

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_&n_students);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User
students=catx(sp, of STUDENT_:);

Depends on your naming structure to some degree. This blog post covers some of the options and how they work:

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 


@Neeydchi wrote:

Hi! I'm trying to automate a piece of code that works well.

 

My goal is to list all the students who did an activity, but I need to report this on a list within my current table. So, I use the following code:

 

 

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_45);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

But I don't want to have to indicate the number of students manually, as this has caused me to generate many errors. So I'm trying to do it with the following code, but it's not working. Can someone help me?

 

Attempt using &n_students:

 

PROC SQL noprint;
	SELECT  count(*) 
	INTO :n_students
	FROM WORK.class_activity;
	QUIT;

data students_list;
set WORK.students;
SP='; ';
students=catx(sp, of STUDENT_1-STUDENT_&n_students);
run;

PROC SQL;
Create Table final as
select t1.*, t2.students from activity t1, students_list t2;
Quit;

 

 

 


 

Tom
Super User Tom
Super User

But how did you get to have multiple "student" variables to begin with?

Why isn't the data organized to have one variable for student and multiple observations if there are multiple students?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2 replies
  • 440 views
  • 0 likes
  • 3 in conversation