BookmarkSubscribeRSS Feed
veeresh10
Calcite | Level 5

Need to combine more than 10 obs data into one observation.

problem is we not have any key to hold as group var. 

 

I have more then 

24 var s

a b c d e f g t h i ...... aa ab ac ad 

 

need to combine multiple observations into one observation.

ex- dataset contains 20 or more variables and 10 to 15 observations. 

so all observations combine it into one single observation.

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

What should that single observation contain? Show us some representable sample of your data please.

veeresh10
Calcite | Level 5

All variables are character variables and contain data with a max length of 50. 

 

 

Shmuel
Garnet | Level 18

If you want all text in one long variable compressed to single space between words in the order of the input lines than try next code:

libname mytxt xlsx "<path and name of file>.xlsx";
data have;
 set mytxt.<sheet_name>;
run;

%let cols = 50; /* adapt to max columns read */
data want(keep=wanted);
   length wanted $10000; /* adapt to max length needed */
   retain wanted ;
   set have end=eof;
        array all {*} $ _ALL_; 
        do i=1 to dim(all);
if not missing(all(i)) then
all(i) = compbl(all(i)); wanted = cats(wanted, all(i)); end; if eof then output; run;
veeresh10
Calcite | Level 5

your code working great with the concept of hold values into one final variable. But my requirement is to club obs into one obs and bring all obs values into one obs value with space with respect to the columns.  

 

I have attached a sheet with the expected result Kindly help on this.

Shmuel
Garnet | Level 18

I see, you want to concatenate all text of the same column, so finally the output will contain all the variables as in the input:

%let cols = 50; /* adapt to max columns read */
data want;
 set have end=eof;
       length txtout1-txtou&cols $80;  /* adapt to max length needed per column */
       array colin {*} $ col1-col&cols;
       array txtout {*} $ txtout1-txtou&cols;
       do i=1 to &cols;
            txtout(i) = cats(txtout(i), colin(i));
       end;
       if eof then output;
      keep txtout1-txtou&cols ;
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
  • 5 replies
  • 2454 views
  • 0 likes
  • 3 in conversation