BookmarkSubscribeRSS Feed
rvsidhu035
Quartz | Level 8

 i am attached sas editor file below
Every id visit in three times each visit conduct four test a b c d in every vist
and second data "b"  give three vist of date.
how merge date varable of data b  with data a dataset.
i want like this
1 a 1 aa
1 b 2 aa
1 c 1 aa
1 d 1 aa
1 a . bb
1 b 2 bb
1 c . bb
1 d 2 bb
.... like this;
data a;
input id test$ sev @@;
datalines;
1 a 1 1 b 2 1 c 1 1 d 1
1 a . 1 b 2 1 c . 1 d 2
1 a 2 1 b 2 1 c 1 1 d 1
2 a 1 2 b . 2 c 1 2 d 2
2 a 1 2 b 1 2 c . 2 d 2
2 a 2 2 b 1 2 c 1 2 d 1
3 a 1 3 b 1 3 c 1 3 d 2
3 a 2 3 b 2 3 c 2 3 d 1
3 a . 3 b 1 3 c 1 3 d 2
;
run;

data b;
input id date$ @@;
datalines;
1 aa 1 bb 1 cc
2 dd 2 ee 2 ff
3 ee 3 gg 3 df
;
run;

1 REPLY 1
Kurt_Bremser
Super User

As long as you can be sure that there's always four entries in A for every entry in B, then this will do it:

data want;
set b;
do i = 1 to 4;
  set a;
  output;
end;
drop i;
run;

A more robust version uses transpose:

data int1;
set a;
by id;
if first.id then visit = 0;
if test = 'a' then visit + 1;
run;

proc transpose data=int1 out=int2 (drop=_name_);
var sev;
id test;
by id visit;
run;

data int3;
set b;
by id;
if first.id
then visit = 1;
else visit + 1;
run;

data int4;
merge
  int2
  int3
;
by id visit;
run;

data want (keep=id test sev date);
set int4;
array vars {4} a b c d;
do i = 1 to 4;
  test = vname(vars{i});
  sev = vars{i};
  output;
end;
run;

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
  • 1 reply
  • 766 views
  • 1 like
  • 2 in conversation