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

Suppose one has the following:

 

i_x	        N
_22606_0	20
_20404_6	3
_21001_0	3
_21301_4	14
_21605_0	14
_22501_0	10
_22602_6	10
_21105_1	1
_20706_0	16
_21603_0	4
_22201_4	4

The desire is to get a total sum of N for each of the individual character values.

 

What would be the easiest way to do that in SAS?

 

Thanks!

 

Nicholas Kormanik

 

1 ACCEPTED SOLUTION

Accepted Solutions
user24feb
Barite | Level 11

but your char's are distinct.

 

Data Have;
  Infile Datalines DELIMITER='09'x;
  Input  i_x $ N;
  Datalines;
_22606_0	20
_20404_6	3
_21001_0	3
_21301_4	14
_21605_0	14
_22501_0	10
_22602_6	10
_21105_1	1
_20706_0	16
_21603_0	4
_22201_4	4
;

Proc Means Data=Have NoPrint NWay;
  By Notsorted i_x;
  Var N;
  Output Out=Have_Sum (Drop=_:) Sum=;
Run;

Proc SQL;
  Create Table Have_Sum_SQL As Select i_x,Sum(N) As N From Have Group By i_x;
Quit;

View solution in original post

5 REPLIES 5
user24feb
Barite | Level 11

but your char's are distinct.

 

Data Have;
  Infile Datalines DELIMITER='09'x;
  Input  i_x $ N;
  Datalines;
_22606_0	20
_20404_6	3
_21001_0	3
_21301_4	14
_21605_0	14
_22501_0	10
_22602_6	10
_21105_1	1
_20706_0	16
_21603_0	4
_22201_4	4
;

Proc Means Data=Have NoPrint NWay;
  By Notsorted i_x;
  Var N;
  Output Out=Have_Sum (Drop=_:) Sum=;
Run;

Proc SQL;
  Create Table Have_Sum_SQL As Select i_x,Sum(N) As N From Have Group By i_x;
Quit;
NKormanik
Barite | Level 11

Terrific.  Been scratching my head trying to get Proc Tabulate to do it.

 

Three quick follow-ups. Please briefly explain what the following were used for:

 

NWay
( Drop=_: )
Sum=

 

Thanks very much!

 

 

NKormanik
Barite | Level 11

Makes sense now.  Thanks so much!

 

My final code on this one:

 

proc sort data=nicholas.n_slope_means__7 ;
by i_x;
run;

proc means data=nicholas.n_slope_means__7
noprint
order=freq
maxdec=0
;
by i_x;
var n;

output out=nicholas.means_summary03
(drop=_:)
sum=
n=
min=
p10=
p20=
p30=
median=
p70=
p80=
p90=
max=
/ autoname
;
run;

 

 

ballardw
Super User

You don't say whether you want a report or a dataset for the output.

Here is what I would consider the easiest:

This is a shorter dataset with some value of your i_x with different n to demonstrate. If you need a dataset you could use the out=option on the tables statement in proc freq.

Data Have;
  Infile Datalines ;
  Input  i_x $ N;
  Datalines;
_22606_0	20
_22606_0	12
_20404_6	3
_20404_6	27
_21001_0	3
_21301_4	14
;
run;

proc freq data=have;
   weight n;
   tables i_x;
run;

Proc Summary or means could sum the N values as well

 

proc summary data=have nway;
   class i_x;
   var N;
   output out=want(drop= _:) sum=;
run;
NKormanik
Barite | Level 11

Thanks, ballardw, for contributing another solution.  Works perfectly.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 912 views
  • 3 likes
  • 3 in conversation