First solution: use a macro loop to create multiple statements that dissect the variables:
data have;
input f1 :yymmdd10. f2 f3 :$10. f4 :yymmdd10. f5 f6 :$10.;
cards;
2018-07-09 3 XXXXXXX 2018-07-10 5 YYYYYYY
;
run;
%macro create_datastep(maxvar);
data want;
set have;
format
f_1 yymmddd10.
f_2 5.
f_3 $10.
;
%do i = 1 %to &maxvar./3;
%let i1 = %eval(&i. * 3 - 2);
%let i2 = %eval(&i. * 3 - 1);
%let i3 = %eval(&i. * 3);
f_1 = f&i1.;
f_2 = f&i2.;
f_3 = f&i3.;
output;
%end;
keep f_1 f_2 f_3;
run;
%mend;
%create_datastep(6)
A probably more "elegant" solution would create three arrays in similar %do loops, and then iterate in a data step do loop.
Define an array for f1-f500.
Loop over the array with do i = 1 to 500.
In the loop, do
array{mod(i,3)} = array{i};
if mod(i,3) = 0 then output;
Keep f1-f3.
Add some code to deal with the last group (since 500 is not a multiple of 3)
You can't unless you have a repeating pattern of types and other attributes.
The problem is not the "how", it's the "what". If f1, f4 and f7 have different attributes, you can't (reliably) store the values in the same column.
First solution: use a macro loop to create multiple statements that dissect the variables:
data have;
input f1 :yymmdd10. f2 f3 :$10. f4 :yymmdd10. f5 f6 :$10.;
cards;
2018-07-09 3 XXXXXXX 2018-07-10 5 YYYYYYY
;
run;
%macro create_datastep(maxvar);
data want;
set have;
format
f_1 yymmddd10.
f_2 5.
f_3 $10.
;
%do i = 1 %to &maxvar./3;
%let i1 = %eval(&i. * 3 - 2);
%let i2 = %eval(&i. * 3 - 1);
%let i3 = %eval(&i. * 3);
f_1 = f&i1.;
f_2 = f&i2.;
f_3 = f&i3.;
output;
%end;
keep f_1 f_2 f_3;
run;
%mend;
%create_datastep(6)
A probably more "elegant" solution would create three arrays in similar %do loops, and then iterate in a data step do loop.
It's of course best to fix such an issue when reading from an external file:
data want2;
input f_1 :yymmdd10. f_2 f_3 :$10. @@;
format f_1 yymmddd10.;
cards;
2018-07-09 3 XXXXXXX 2018-07-10 5 YYYYYYY
;
run;
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.