BookmarkSubscribeRSS Feed
Sunny4
Calcite | Level 5

I have data

id

1

2

3

 

Required output is

id   id_1

1    6

2    6

3    6

 

where 6= sum of id's

 

4 REPLIES 4
BrunoMueller
SAS Super FREQ

Interesting idea to sum up ID's.

 

If you know about SQL, you can use Proc SQL to solve this.

 

There are for sure other ways using DATA Step programming or using a Proc such as MEANS.

 

SQL sample:

data have;
  infile cards;
  input id;
cards;
1
2
3
;

proc sql;
  select
    id
    , (select sum(id) from have) as id_1
  from
    have
  ;
quit;

 

For next time: please provide a better subject line for the post, so that we have more information already in the title.

 

Find More information here

https://communities.sas.com/t5/Getting-Started/How-to-get-fast-helpful-answers/ta-p/226133/jump-to/f...

Reeza
Super User

@Sunny4 note that I've modified your subject line to be more descriptive of your question.

art297
Opal | Level 21

Here is one proc+datastep method:

data have;
  input id;
  datalines;
1
2
3
;

proc summary data=have;
  var id;
  output out=sum (drop=_:) sum=id_1;
run;

data want;
  set have;
  if _n_ eq 1 then set sum;
run;

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

And here's a single data step solution.  It's still two passes through the data, but it's a single step.

 

data want;
  set have (in=firstpass)
      have (in=secondpass);
  if firstpass then sumid+id;
  if secondpass;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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