In my input dataset I do have a 28 variables which are corresponding to each other like Left10a, Right10a, Left11a, Right11a......................Left24a, Right24a.
I need to compare its corresponding variables like Left10a & Right10a and create a new variable design10 by using do loop/arrary/something because tomorrow the maximum count of 24 may increase to 30.
I tried with below code but its only working for single iteration which is i =10.
Please help me how do loop have to work till 24 iterations.
Also suggest me is there any other way I can do to create 14 new variables.
Thanks for your time.
data count;
set count2;
%let i = 10;
do i = &i. to 24;
if left&i.a ne "" and righ&i.a ne "" then do;
if left&i.a ^= righ&i.a then design&i. = "NOT EQUAL";
else if left&i.a = righ&i.a then design&i. = "EQUAL";
end;
%let i=%eval(&i + 1);
end;
run;
Regards,
Kumar.
Yeah, by using 3 arrays it worked.
Thank You.
This is the code I used to create new variable dsn but this varaible is truncating and I need to define length statement. Can you help me with this.
data x;
set tst;
array lt {12} $ left01a left02a left03a left04a left05a left06a left07a left08a left09a left10a left11a left12a;
array rt {12} $ righ01a righ02a righ03a righ04a righ05a righ06a righ07a righ08a righ09a righ10a righ11a righ12a;
array dsn {12} $ dsn01a dsn02a dsn03a dsn04a dsn05a dsn06a dsn07a dsn08a dsn09a dsn10a dsn11a dsn12a;
do z = 1 to dim(lt);
if lt(z) ne "" and rt(z) ne "" then do;
if lt(z) ne rt(z) then dsn(z) = "Not Equal";
else if lt(z) eq rt(z) then dsn(z) = "Equal";
end;
end;
run;
For your dsn array, just put a number immediately after the $ to declare a length.
array dsn{12} $50 dsn01a dsn02a. etc.
To make this fully dynamic, I think you would need to know how many righ* or left* variables you have prior to the data step. Pop that into a macro variable and use that to declare your arrays. It's the new variable array that is the problem. Unfortunately, I don't think you can use a variable value for the subscript. If you knew the number of variables, you could declare the arrays like:
%let varnum=25; /* Get this by querying dictionary tables */
data x;
set tst;
array lt{&varnum} $ righ:;
array rt{&varnum} $ left:;
array dsn{&varnum} $9;
......
This will name your new variables dsn1 dsn2 dsn3, etc. If you need them exactly as you have in the example above (dsn01a, dsn02a, etc), then you will need a macro-based approach like @smantha has below.
data count;
set count2;
%let i = 10;
%macro _loop();
%do i = &i. %to 24;
if left&i.a ne "" and righ&i.a ne "" then do;
if left&i.a ^= righ&i.a then design&i. = "NOT EQUAL";
else if left&i.a = righ&i.a then design&i. = "EQUAL";
end;
%end;
%mend _loop;
%_loop;
end;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.