BookmarkSubscribeRSS Feed
skjhzzang
Calcite | Level 5

For example below

 

Peter  2  14  16

James  3  17  19  18

Max  1  7

Logan  5  19  15  11  9  18

 

 

First variable is Name, Second variable is number of visit and others are amounts.

Peter visited twice so there are 2 amount variables and James visited three times so there are 3 amount variables.

I want to create a single data step that can read different number of variables according to a condition (# of visits).

And also sum of amount variables for each person.

Is it possible to just ignore reading Amount variables and just calculate sum of amount variables for each person and running total of the total amount variables for each person in a single data step? 

Ex.)

Name   TotalAmt  Runtotal

Peter    30              30

James  54              84

Max      7                91

Logan  72               163

...

...

 

2 REPLIES 2
Patrick
Opal | Level 21

You can't have a varying number of variables per row. But you can organize your data in a long format and you can have a varying number of rows per name. That's what the code below does.

It's also much easier to work with data in a long format (less variable, more rows). Many of the SAS Procedures then allow you to easily aggregate such data for analysis and reporting purposes.

data sample(drop=_:);
  infile datalines truncover;
  input name $ n_vals @;
  do _i=1 to n_vals;
    input amt @;
    output;
  end;
  datalines;
Peter 2 14 16
James 3 17 19 18
Max 1 7
Logan 5 19 15 11 9 18
;

proc print data=sample;
run;

proc sql;
  select 
    name,
    amt,
    sum(amt) as total
  from sample
  group by name
  ;
quit;
mkeintz
PROC Star

So, do you want to produce a different number of observations for each name (one obs per visit)?  That's what @Patrick has shown you.

 

Or do you want one observation per name?  If would have differing number of non-missing values for a fixed set of variables (where the fixed set would be sufficient to accommodate the largest number of visits in your sample).

--------------------------
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

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

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