I am trying to take the following dataset:
Parent ID DOB Zip Country
123 201303 93490 US
123 201107 93940 US
123 200912 93940 US
and turn it into
123 201303 93490 US 201107 93940 US 200912 93940 US
I tried this:
ARRAY ZIP {*} $ ZIP1-ZIP4;
ARRAY CNTRY {*} $ CNTRY1-CNTRY4;
ARRAY DOB_ {*} $ DOB1-DOB4;
IF FIRST.ID & LAST.ID THEN DO;
ZIP1 = ADD_ZIP; CNTRY1 = ADD_CNTRY;
DOB1 = DOB;
END;
ELSE DO UNTIL (LAST.ID);
DO X = 1 TO DIM(ZIP);
ZIP{X} = ADD_ZIP; DOB_{X} = DOB; CNTRY{X}=ADD_CNTRY;
END;
END;
IF LAST.ID THEN OUTPUT;
This does not work - I ID had two distinct records and it returned ZIP1-ZIP4, DOB1-DOB4, CNTRY1-CNTRY4 instead of just 1st block and 2nd block with data and blocks 3 and 4 empty.
I know that somehow I need to advance ID to next line as X increases, but I am lost. Help, please ?
Scott, from Calif.
I have adapted your code to fix it:
data want(drop=i dob add_zip add_cntry);
set have;
retain i zip1-zip4 CNTRY1-CNTRY4 DOB1-DOB4;
ARRAY ZIP {*} $ ZIP1-ZIP4;
ARRAY CNTRY {*} $ CNTRY1-CNTRY4;
ARRAY DOB_ {*} $ DOB1-DOB4;
if first.ID then do;
i=1;
call missing(of zip(*));
call missing(of cntry(*));
call missing(of dob_(*));
end;
ZIP(i) = ADD_ZIP;
CNTRY(i) = ADD_CNTRY;
DOB_(i) = DOB;
IF LAST.ID THEN OUTPUT;
i+1;
run;
I have adapted your code to fix it:
data want(drop=i dob add_zip add_cntry);
set have;
retain i zip1-zip4 CNTRY1-CNTRY4 DOB1-DOB4;
ARRAY ZIP {*} $ ZIP1-ZIP4;
ARRAY CNTRY {*} $ CNTRY1-CNTRY4;
ARRAY DOB_ {*} $ DOB1-DOB4;
if first.ID then do;
i=1;
call missing(of zip(*));
call missing(of cntry(*));
call missing(of dob_(*));
end;
ZIP(i) = ADD_ZIP;
CNTRY(i) = ADD_CNTRY;
DOB_(i) = DOB;
IF LAST.ID THEN OUTPUT;
i+1;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.