BookmarkSubscribeRSS Feed
Gator77
Calcite | Level 5
Hello,

I’m working on a rather large data set that I need to add one value to several hundred other values in the same row.

And example would be:

ID Wait Time Pts 1 Pts 2 Pts 3 etc.
1 12.85 1.67 2 1.5.

Output
14.52 14.85 14.35

There has to be an easier way to do this without manually inputting formulas one by one for each value.

Thanks
4 REPLIES 4
unison
Lapis Lazuli | Level 10

Hi there, something like this solve your problem?

 

data have;
input id wait_time pts_1 pts_2 pts_3;
datalines;
1 12.85 1.67 2 1.5
;
run;

data want;
set have;
array pts pts:;
do over pts;
pts=pts+wait_time;
end;
run;

-unison

-unison
mkeintz
PROC Star

@unison wrote:

Hi there, something like this solve your problem?

 

data have;
input id wait_time pts_1 pts_2 pts_3;
datalines;
1 12.85 1.67 2 1.5
;
run;

data want;
set have;
array pts pts:;
do over pts;
pts+wait_time;
end;
run;

-unison


The statement

pts+wait_time;

will work for this problem, as stated.  But I would NOT recommend it.  This is a SAS SUMming statement, which means (1) is will accumulated values over consecutive observations (not a problem here), and (2) will not always produce the same results as

pts=pts+wait_time;

Take a look at the results for observations 3 and 4 below, where there are missing values introduced:

 

data have; 
input id wait_time pts_1 pts_2 pts_3; 
datalines; 
1 12.85 1.67 2 1.5 
2 11.85 1.67 2 1.5
3 10.85    . 2 1.5 
4     . 1.67 2 1.5
run; 
data want1; 
  set have; 
  array pts pts:; 
  do over pts; 
    pts+wait_time; 
  end; 
run; 
data want2; 
  set have; 
  array pts pts:; 
  do over pts; 
    pts=pts+wait_time; 
  end; 
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

--------------------------
Reeza
Super User

Arrays are the recommend approach here. 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 


@Gator77 wrote:
Hello,

I’m working on a rather large data set that I need to add one value to several hundred other values in the same row.

And example would be:

ID Wait Time Pts 1 Pts 2 Pts 3 etc.
1 12.85 1.67 2 1.5.

Output
14.52 14.85 14.35

There has to be an easier way to do this without manually inputting formulas one by one for each value.

Thanks

 

ballardw
Super User

@Gator77 wrote:
Hello,

I’m working on a rather large data set that I need to add one value to several hundred other values in the same row.

And example would be:

ID Wait Time Pts 1 Pts 2 Pts 3 etc.
1 12.85 1.67 2 1.5.

Output
14.52 14.85 14.35

There has to be an easier way to do this without manually inputting formulas one by one for each value.

Thanks

I think that you need to walk us through the explicit formula you are using. The input data has 5 values and the output only 3. So which variables are used and how to get that output??

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