BookmarkSubscribeRSS Feed
pensarchem
Obsidian | Level 7

data test;

input clnt_label $ cd $;

datalines;

 

1234567 740

1234567 750

7654321 760

7654321 780

;

run;

 

I hope to change the format to :

clnt_label   cd1 cd2

1234567    740 750

7654321    760 780

 

In real word, there are a few hundreds of cd codes and millions of records, is there a good way to transform the table as I hope, thanks.

6 REPLIES 6
novinosrin
Tourmaline | Level 20

"not transpose though"-- How do i comprehend this? Can you please explain what you mean

pensarchem
Obsidian | Level 7

Never mind, thanks for you attention.  Transpose works fine, I misunderstood it..

 

proc transpose data=test;

by clnt_label;

var diagcd;

run;

novinosrin
Tourmaline | Level 20

data test;
input clnt_label $ cd $;
datalines;
1234567 740
1234567 750
7654321 760
7654321 780
;
proc sql noprint; 
 select max(obs) into :obs 
from (select count(*) as obs from test group by clnt_label) ;
quit;

proc summary nway data=test missing; 
 class clnt_label; 
 output out = want(drop=_type_ _freq_) 
 idgroup(out[&obs](cd)=) ;
run;

/*or*/



data test;
input clnt_label $ cd $;
datalines;
1234567 740
1234567 750
7654321 760
7654321 780
;

proc transpose data=test out=want(drop=_name_) prefix=cd;
by clnt_label;
var cd;
run;
Kurt_Bremser
Super User

This IS a transposition.

First, build the id variable, then transpose:

data pretrans;
set have;
by clnt_label;
if first.clnt_label
then count = 1;
else count + 1;
run;

proc transpose data=pretrans out=want prefix=cd;
by clnt_label;
id count;
var cd;
run;

Not tested, as I'm on my tablet.

PGStats
Opal | Level 21

You just need an intermediate step (note: defining a view doesn't create a dataset):

 

data test;
input clnt_label $ cd $;
datalines;
1234567 740
1234567 750
7654321 760
7654321 780
;

data temp / view=temp;
do i = 1 by 1 until(last.clnt_label);
	set test; by clnt_label notsorted;
	output;
	end;
run;

proc transpose data=temp out=want(drop=_name_) prefix=cd;
by clnt_label notsorted;
var cd;
id i;
run;

proc print data=want noobs; run;
PG
mkeintz
PROC Star

If you are reading raw data and you always have exactly 2 obs per clnt_label, then :

 

data want (drop=dummy);
  input clnt_label $ cd1 $ / dummy $  cd2 $;
datalines;
1234567 740
1234567 750
7654321 760
7654321 780
run;

 

But if you want to convert data set TEST without using proc transpose (but why?), then:

data test;
input clnt_label $ cd $;
datalines;
1234567 740
1234567 750
7654321 760
7654321 780
run;
data want (drop=_:);
  do _i=1 by 1 until (last.clnt_label);
    set test (rename=(cd=_cd));
	by clnt_label notsorted;
    array cd {2} $4 ;
    cd{_i}=_cd;
  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

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

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