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

Hello,

 

I have a data set that looks like this and I want to transpose it to have only one observation for each ID.

 

idcol1
11
11
10
20
20
31
4.
4.
41
41
40
5

1

 

When I do PROC transpose with the code below, I get this:

proc transpose data=have out=want prefix = variable;
	by id;
	var col1;
run;
idvariable1variable2variable3variable4variable5
1110..
200...
31....
4..110
51....

 

For ID 1 to 3 and 5, everything is okay but my question is for ID = 4. Is there a way to ignore the missing value in the the first two variables and assign variable 3 instead?

 

Here is what I want (I will eventually remove the variable that are completely empty)

idvariable1variable2variable3variable4variable5
1110..
200...
31....
4110..
51....

 

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
V_27
Obsidian | Level 7

 

Hi @dera,

Try sorting id in ascending and col1 in descending order. Check the given sas code.

proc sort data=have out=new;
by id descending col1;
run;

proc transpose data=new out=want prefix=variable;
	by id;
	var col1;
run;

View solution in original post

6 REPLIES 6
V_27
Obsidian | Level 7

 

Hi @dera,

Try sorting id in ascending and col1 in descending order. Check the given sas code.

proc sort data=have out=new;
by id descending col1;
run;

proc transpose data=new out=want prefix=variable;
	by id;
	var col1;
run;
dera
Obsidian | Level 7
Thanks V_27! It works perfectly.
V_27
Obsidian | Level 7

 

You are most welcome @dera Smiley Happy

PeterClemmensen
Tourmaline | Level 20

I think you have to perform the operation afterwards, like this

 

data have;
input id col1;
datalines;
1 1
1 1
1 0
2 0
2 0
3 1
4 .
4 .
4 1
4 1
4 0
5 1
;

proc transpose data=have out=want prefix = variable;
	by id;
	var col1;
run;

data final (drop=variable1-variable5 j i);
   set want;
   array in variable1-variable5;
   array out $ var1-var5;
   j=1;
   do i=1 to 5;
      if in(i) ne ' ' then do;
         out(j)=in(i);
         j+1;
      end;
   end;
run;
novinosrin
Tourmaline | Level 20
data have;
input id	col1;
cards;
1	1
1	1
1	0
2	0
2	0
3	1
4	.
4	.
4	1
4	1
4	0
5 1 
;
data want;
do  until(last.id);
set have;
by id ;
array  variable(5);
if first.id then call missing(of variable(*),_c);
if not missing(col1) then do;
_c+1;
variable(_c)=col1;
end;
end;
drop _: col1;
run;




 
FreelanceReinh
Jade | Level 19

Hello @dera,

 

What if the first COL1 value of ID 4 was not missing? Would you still ignore the missing value in the second observation of this ID?

If so, you could simply insert a WHERE statement into your PROC TRANSPOSE step, e.g.

where col1>.;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 11661 views
  • 1 like
  • 5 in conversation