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

I have a set of data similar to below. I want to move the last two variables down underneath ID1 and Description1 and retain the first  variable.

 

Have

DateNameID1Description1ID2Description2
29-Apr-17Claire123100. 
04-Apr-17David456200555300
05-Apr-17David456200555300
06-Apr-17David456200555300

06-Apr-17

David456200555300
24-Apr-17David456200555300
25-Apr-17David456200555300
25-Apr-17David456200555300
26-Apr-17David456200555300
26-Apr-17David456200555300
27-Apr-17David456200555300

 

 

Want:

 

DateNameID1Description1
29-Apr-17Claire123100
04-Apr-17David456200
05-Apr-17David456200
06-Apr-17David456200
06-Apr-17David456200
24-Apr-17David456200
25-Apr-17David456200
25-Apr-17David456200
26-Apr-17David456200
26-Apr-17David456200
27-Apr-17David456200
04-Apr-17David555300
05-Apr-17David555300
06-Apr-17David555300
06-Apr-17David555300
24-Apr-17David555300
25-Apr-17David555300
25-Apr-17David555300
26-Apr-17David555300
26-Apr-17David555300
27-Apr-17David555300

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Should be straightforward:

data want (drop=id1 id2 description1 description2);
  set have;
  id=id1;
  description=description1;
  output;
  id=id2;
  description=description2;
  output;
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Should be straightforward:

data want (drop=id1 id2 description1 description2);
  set have;
  id=id1;
  description=description1;
  output;
  id=id2;
  description=description2;
  output;
run;
westwood49
Calcite | Level 5

Worked perfectly, thank you so much.

Kurt_Bremser
Super User

A less deterministic solution uses a double transpose:

data have;
infile cards truncover dlm='09'x;
input Date :date9. Name $ ID1 Description1 ID2 Description2;
format date yymmddd10.;
record_id = _n_; * we need this for the tranposes;
cards;
29-Apr-17	Claire	123	100	.	 
04-Apr-17	David	456	200	555	300
05-Apr-17	David	456	200	555	300
06-Apr-17	David	456	200	555	300
06-Apr-17	David	456	200	555	300
24-Apr-17	David	456	200	555	300
25-Apr-17	David	456	200	555	300
25-Apr-17	David	456	200	555	300
26-Apr-17	David	456	200	555	300
26-Apr-17	David	456	200	555	300
27-Apr-17	David	456	200	555	300
run;

proc transpose data=have out=int1;
by record_id date name;
var id: desc:;
run;

data int2;
set int1;
sub_id = substr(_name_,length(_name_));
_name_ = substr(_name_,1,length(_name_)-1);
run;

proc sort data=int2;
by record_id sub_id;
run;

proc transpose data=int2 out=want (drop=record_id _name_);
by record_id date name sub_id;
var col1;
id _name_;
run;
Astounding
PROC Star

Since you requested that the original variable names (ID1 and Description1) remain:

 

data want;

set have;

output;

if ID2 > . then do;

   id1 = id2;

   description1 = description2;

   output;

end;

keep date name id1 description1;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 696 views
  • 1 like
  • 4 in conversation