BookmarkSubscribeRSS Feed
SAS_Question
Quartz | Level 8

I HAVE (see the import file have.csv) and this code to import it: 

data work.HAVE;
/* change path to fit your setup! */
  infile "D:\GRADES\have.csv" 
	delimiter = "|"
	missover 
	dsd
	firstobs=2;
 
	informat ID $20.;
	informat COURSE $20.;
	informat DATE YYMMDD10.;
	informat EARNED_CREDIT $20.;
	informat CRS_COMP $20.;
	informat C_GRADE $20.;
	informat N_GRADE 4.;
	informat CREDIT 4.;

	format ID $20.;
	format COURSE $20.;
	format DATE YYMMDD10.;
	format EARNED_CREDIT $20.;
	format CRS_COMP $20.;
	format C_GRADE $20.;
	format N_GRADE 4.;
	format CREDIT 4.;

	input
	ID
	COURSE 
	DATE 
	EARNED_CREDIT
	CRS_COMP
	C_GRADE
	N_GRADE
	CREDIT
;
run;

I WANT: 

The Credits (TotalCreditEarned) and ALL Grades (as (chance_1,Chance_2,Chance_n) in each column apart) of each student to show up like this in a proc print.

How do I do this? 

 

WANT.png

 

9 REPLIES 9
andreas_lds
Jade | Level 19

Try proc report: group by student and course, sum for credit and across for n_grade.

Ksharp
Super User
data work.HAVE;
/* change path to fit your setup! */
  infile "c:\temp\have.csv" 
 delimiter = "|"
 missover 
 dsd
 firstobs=2;
 
 informat ID $20.;
 informat COURSE $20.;
 informat DATE DDMMYY10.;
 informat EARNED_CREDIT $20.;
 informat CRS_COMP $20.;
 informat C_GRADE $20.;
 informat N_GRADE 4.;
 informat CREDIT 4.;

 format ID $20.;
 format COURSE $20.;
 format DATE YYMMDD10.;
 format EARNED_CREDIT $20.;
 format CRS_COMP $20.;
 format C_GRADE $20.;
 format N_GRADE 4.;
 format CREDIT 4.;

 input
 ID
 COURSE 
 DATE 
 EARNED_CREDIT
 CRS_COMP
 C_GRADE
 N_GRADE
 CREDIT
;
run;

data part1(index=(x=(id course)));
 set have;
 if CREDIT=0;
run;
proc transpose data=part1 out=part2(drop=_: course) prefix=Chance_;
by id course;
var C_GRADE;
run;

data part3(index=(x=(id course)) rename=(CREDIT=TotalCreditEarned));
 set have;
 if CREDIT ne 0;
 keep id course CREDIT;
run;


data want;
 merge part3 part2;
 by id;
run;
SAS_Question
Quartz | Level 8

@Ksharp .. you really deserve a diamond as sharp as your Knowledge for your programming skills! Wow!!  🙂

I'm waiting with accepting your working solution though.

Because I'm really eager to learn more about proc report and how to solve this HAVE data of mine with a single Proc Report...

Thanks for your answer though. That's a really neat solution too!!!! 

 

@andreas_lds thank you for the tip. Can you give an example of this Proc Report you are mentioning? Because I'm still learning this proc I would really love to see an example of how you would do it. Thanks a lot in advance! 

 

Reeza
Super User

The solution depends on what type of output you want. If you want only displayed output, piped to Excel/PDF/WORD use PROC REPORT. If you want it in that format in a data set to do further analysis, then @Ksharp solution is the one you want. 

 

 

 

 

SAS_Question
Quartz | Level 8
I would love to see the first output you mentioned @Reeza !! Thank you for your clarification on this matter!
PaigeMiller
Diamond | Level 26

@SAS_Question wrote:
I would love to see the first output you mentioned @Reeza !! Thank you for your clarification on this matter!

Examples of using ACROSS in PROC REPORT.

https://communities.sas.com/t5/SAS-Procedures/Proc-report-ACROSS/td-p/433732

--
Paige Miller
Reeza
Super User

Here's a starter. Based on this, I suspect you have to do some more data wrangling to get the PROC REPORT exactly correct.

FYI - I had to change the import step to correctly read the data, the Date informat was incorrect and c_grade was being read in as a character when it should be numeric. I'm assuming....

 

data work.HAVE;
/* change path to fit your setup! */
  infile "/home/fkhurshed/Demo1/HAVE.csv" 
	delimiter = "|"
	missover 
	dsd
	firstobs=2;
 
	informat ID $20.;
	informat COURSE $20.;
	informat DATE DDMMYY10.;
	informat EARNED_CREDIT $20.;
	informat CRS_COMP $20.;
	informat C_GRADE commax.;
	informat N_GRADE 4.;
	informat CREDIT 4.;

	format ID $20.;
	format COURSE $20.;
	format DATE YYMMDD10.;
	format EARNED_CREDIT $20.;
	format CRS_COMP $20.;
	format C_GRADE 8.2;
	format N_GRADE 4.;
	format CREDIT 4.;

	input
	ID
	COURSE 
	DATE 
	EARNED_CREDIT
	CRS_COMP
	C_GRADE
	N_GRADE
	CREDIT
;
run;

proc sort data=have;
by id course;
run;

data have_indexed;
set have;
by id course;
*where credit ne 0;
if first.course or first.id then chance=1;
else chance+1;
run;


proc report data=have_indexed;
*where credit ne 0;
column ID course credit (chance , c_grade)  ;
define id / group;
define course /group;
define chance / across ;
define credit / sum;
define c_grade / '' sum format=8.1;
run;
SAS_Question
Quartz | Level 8

Thank you @Reeza !!! That's what I mean. Only... 1 addition please..if you don't mind..? 

How do I get a sum (of 25 CREDITS for each student) on these 2 places of the CREDITS? Can you point me out in the right direction? TiA!! 

 

SAS_Question_0-1660735646773.png

 

Reeza
Super User

Give a try and happy to help. Look into a COMPUTE statement with a BREAK statement as well.

 

https://excursive.org/sas/ProcReportPaper.pdf

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 812 views
  • 5 likes
  • 5 in conversation