BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasmhe1
Calcite | Level 5

I'm new to SAS, am starting to watch the beginner tutorials, and would appreciate advice on implementing this problem:

 

Given existing dataset1:

  USER   ACRES

  John       5

  John       7

  Tom        3

  Tom        4

  Tom        8

  (and more)

I'd like SAS to create dataset2, containing one record per USER containing total acres:

 

  USER    TOTAL_ACRES

  John             12

  Tom              15

  (and more)

 

No report, just a new dataset (dataset2).

Advice would be appreciated.  Or perhaps there's already a tutorial for this?       

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way to create data set.

proc summary data=dataset1 nway;
   class user;
   var acres;
   output out=dataset2 (drop=_:) sum=total_acres;
run;

View solution in original post

6 REPLIES 6
ballardw
Super User

One way to create data set.

proc summary data=dataset1 nway;
   class user;
   var acres;
   output out=dataset2 (drop=_:) sum=total_acres;
run;
sasmhe1
Calcite | Level 5

Thank you, ballardw.

I tried the code in SAS Studio, but got: Libref .. is not assigned.

I'll continue with the beginner tutorials to understand this message, and if I don't succeed, I'll return with another question!

Thank you again.

ballardw
Super User

Show the entire log of what you submitted.

 

Libref means you attempted to use a library that is not defined in your current session. There is no we would know anything about what libraries you have. Basically you would have had to use something like data= some.dataset1  The . tells SAS that SOME is supposed to be the name of a library, physical storage location for the dataset named Dataset1.

Normally a LIBNAME statement is provided to create a library reference (libref).

sasmhe1
Calcite | Level 5

Hi ballardw,

Your method worked (I got around the libref problem).

Now I need to understand why it worked (may take a while).

Thank you again.

P.S.  Thanks also to the other responses, as I'd like to learn those methods also.

Reeza
Super User

I highly recommend learning and understanding PROC MEANS. Pick one proc, learn it well, then learn other ways. PROC MEANS is incredibly versatile and can do many different summary statistics so it's a great place to start.

 

Here's a decent quick walkthrough of the features

https://sascrunch.com/proc-means/

 

 

 

 

 

 

Kurt_Bremser
Super User

While using PROC SUMMARY is the recommended way, here two other ways using SQL or a SORT/DATA step combination:

proc sql;
create dataset2 as
  select
    user,
    sum(acres) as total_acres
  from dataset1
  group by user
;
quit;
proc sort data=dataset1;
by user;
run;

data dataset2;
set dataset1;
by user;
if first.user
then total_acres = acres;
else total_acres + acres;
if last.user; /* subsetting IF, causes only one output per group */
keep user total_acres;
run;

The statement in the ELSE branch is a SUM Statement  and implies an automatic RETAIN of the summed variable.

 

The DATA step method is often needed when you need to do accumulations of values which cannot be handled with the summary functions of SQL or the statistics available in SUMMARY. Think concatenation of strings or determining presence of certain "vertical" value combinations.

 

The DATA step can also be done with a DO UNTIL loop:

data dataset2;
do until (last.user);
  set dataset1;
  by user;
  if first.user
  then total_acres = acres;
  else total_acres + acres;
end;
keep user total_acres;
run;

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
  • 6 replies
  • 420 views
  • 2 likes
  • 4 in conversation