BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bknitch
Quartz | Level 8

I have a data set that's been transposed already. I'm trying to find a way to replace the 1's with the numeric value it corresponds to in the variable field. I'm trying to un-transpose the variable and make the table long and skinny rather than wide. I'm also trying to replace the variable names with one name as 'COND_C'.

 

There are roughly 90 variables that start at CC1 and end in CC189. I tried it with an if statement but it did not capture it correctly. 

 

ID CC1 CC2 CC6 CC8 CC9 CC100 CC108
123 1 0 0 0 0 1 0
124 0 1 0 0 0 1 0
125 1 0 0 1 0 0 0

 

This is what I would like the output to look like. 

ID COND_C
123 1
123 100
124 2
124 100
125 1
125 8
1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
data have;
input ID$ CC1	CC2	CC6	CC8	CC9	CC100	CC108;
cards;
123 1 0 0 0 0 1 0
124 0 1 0 0 0 1 0
125 1 0 0 1 0 0 0
;
run;

data want;
set have;
array C_[*]  CC:;

do i = 1 to dim(C_);
	if C_[i] > 0 then 
		COND_C=input(compress(vname(C_[i]),'CC'),8.);
	else continue;
	output;
end;

drop CC:;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Do a wide to long proc transpose and filter out all the 0's.

proc transpose data=have out=want(where=(col1 ne 0) rename = _name_ = cond_c);
by ID;
var CC1-CC189;
run;
r_behata
Barite | Level 11
data have;
input ID$ CC1	CC2	CC6	CC8	CC9	CC100	CC108;
cards;
123 1 0 0 0 0 1 0
124 0 1 0 0 0 1 0
125 1 0 0 1 0 0 0
;
run;

data want;
set have;
array C_[*]  CC:;

do i = 1 to dim(C_);
	if C_[i] > 0 then 
		COND_C=input(compress(vname(C_[i]),'CC'),8.);
	else continue;
	output;
end;

drop CC:;
run;
bknitch
Quartz | Level 8
This was perfect, thank you for this!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1076 views
  • 3 likes
  • 3 in conversation