BookmarkSubscribeRSS Feed
MarkWik
Quartz | Level 8

I'd like to understand how to combine obs from different records into a single observation in the output?For example, in sashelp.class or using some of your own input, can anybody demonstrate with a code how to accomplish the same?

1. combine 4,6,and 8 to the 3rd observation?

2. combine 2nd obs, and last observation to 1st obs.

What i really am after is i wanna understand how you look prev once you are past that iteration in the implied datastep loop. Examples i would appreciate is doing the sample in 1. multiple pass and 2. in single pass. Any kind of simple, medium and complex examples will really help.

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Provide some test data and required output.  There are many logical methods of looking up observations which fit into many differing scenarios - hash tables, merge statements, have a set in a conditional branch, sub-querying in sql, lag() function, by group processing, arrays etc.  Say for instance your first options - combine 4,6,8 to 3rd observation - what does this actually mean.  The position of a row in a dataset is purely arbitrary, so if I sort that dataset maybe row 8 will be in position 3.  I wouldn't recommend to use arbitrary row number as a means to identify specific data.

If however row 4, 5, 6 contains a id variable for "sum" and row 3 has the id "total" then we could do:

data have;

  idvar="not used"; output;

  idvar="not used"; output;

  idvar="total"; result=.; output;

  idvar="sum"; result=4; output;

  idvar="sum"; result=3; output;

  idvar="sum"; result=6; output;

run;

proc sql;

  create table WANT as

  select  IDVAR,

          (select SUM(RESULT) from WORK.HAVE where IDVAR="sum") as RESULT

  from    WORK.HAVE

  where   IDVAR="total";

quit;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1 reply
  • 301 views
  • 0 likes
  • 2 in conversation