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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ketpt42
Quartz | Level 8
You're mixing macro variables with data step code. It's not going to work in the way you are intending. You need three arrays. One each for your left, righ and (new) design variables. Then use a do loop to traverse through the variables using dim(<left array>) as a stopping point. Test the equality within the do loop and set the corresponding value in the design variable.

View solution in original post

6 REPLIES 6
ketpt42
Quartz | Level 8
You're mixing macro variables with data step code. It's not going to work in the way you are intending. You need three arrays. One each for your left, righ and (new) design variables. Then use a do loop to traverse through the variables using dim(<left array>) as a stopping point. Test the equality within the do loop and set the corresponding value in the design variable.
Kumar6
Obsidian | Level 7

Yeah, by using 3 arrays it worked.

 

Thank You.

Kumar6
Obsidian | Level 7

 

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;

ketpt42
Quartz | Level 8

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.

smantha
Lapis Lazuli | Level 10
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;
Kumar6
Obsidian | Level 7
Thank You.

Regards,
Kumar.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 686 views
  • 3 likes
  • 3 in conversation