BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hjjijkkl
Pyrite | Level 9
 
ID  COL1 CoL2 COL3  COL4  
1   04/03/2020      

I got a table like this after using proc transpose to convert horizontal variable to vertical. I wanted to know how to rename the variables COL1 COL2 COL3 COL4 to one variable name ''Date".

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You have a dataset with 4 variables, each containing a value.  You can't RENAME 4 variables to the same name.  You apparently want to do something else.   For instance, in the transposed data set, if you always have only one valid data value among the 4 variables, with the other 3 always missing, you could do something like this in a DATA step reading in the transposed data:

 

data new;
   set transposed;
   new_var=coalesce(co1,co2,col3,col4);
   format new_var date9.;
run;

The coalesce function takes the first non-missing value from the list of arguments.

--------------------------
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

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

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You have a dataset with 4 variables, each containing a value.  You can't RENAME 4 variables to the same name.  You apparently want to do something else.   For instance, in the transposed data set, if you always have only one valid data value among the 4 variables, with the other 3 always missing, you could do something like this in a DATA step reading in the transposed data:

 

data new;
   set transposed;
   new_var=coalesce(co1,co2,col3,col4);
   format new_var date9.;
run;

The coalesce function takes the first non-missing value from the list of arguments.

--------------------------
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

--------------------------
mklangley
Lapis Lazuli | Level 10

Or alternatively, you can transpose your data so that you have two columns: ID and Date, with a row for each date.

data have;
    input id col1 :mmddyy10. col2 :mmddyy10. col3 :mmddyy10. col4 :mmddyy10.;
    format col1-col4 mmddyy10.;
    datalines;
    1 . 04/03/2020 . .
    ;
run;

proc transpose data=have out=want (drop=_: rename=(col1 = date));
    by id;
run;

Result:

id	date
1	.
1	04/03/2020
1	.
1	.

It all depends on what you want, and whether or not there can be more than one date per ID, like @mkeintz mentioned.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1053 views
  • 0 likes
  • 3 in conversation