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

Hi all,

similarly like this step i have write 24 steps

 

if shcheck1 = 1 then do;
TERM =Strip(sb1_display);
CAT ='HISTORY' ;
output;
end;

 

like this 

if shcheck2 = 1 then do;
TERM =Strip(sb2_display);
CAT ='HISTORY' ;
output;
end;

 

if shcheck3 = 1 then do;
TERM =Strip(sb3_display);
CAT ='HISTORY' ;
output;
end;

is there any easy way to give it in a macro for the numbers changing till 24? 

Thanks in adv

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Good that you asked. Try to avoid SAS macro language until you're sufficiently familiar with SAS Base language. 

Base SAS Arrays should give you what you need.

data want(drop=_:);
  set have;
  array shchecks {*} shcheck1 - shcheck24;
  array sb_displays {*} sb1_display -- sb24_display;

  do _i=1 to dim(shchecks);
    if shchecks[_i] = 1 then
      do;
        TERM =Strip(sb_displays[_i]);
        CAT ='HISTORY';
        output;
      end;
  end;
run;

 

Ideally numbered variables have the number at the end as this makes them easy to reference. 

In above code "sb<n>_display" got the numbering in the middle so here I couldn't just reference as a list.

Assuming that the variables come one after another in the source dataset I've used the double dash notation. You need to check in your source data if my assumption is correct (use a proc contents on the source table with order=varnum; check in the result if the sb<n> variable really come one after the other with consecutive varnums). If the sb<n> vars are not in consecutive order then you need to write them explicitly in the array definition - like:

array sb_displays {*} sb1_display sb2_display sb3_display ....;

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

You don't need macro here. Not tested :

 

data want ;
    set have;

    array shcheck shcheck:;

    do over shcheck;
        if shcheck=1 then do;
            TERM=strip(vvaluex(cats('sb',_i_,'_display')));
            CAT="HISTORY";
            output; 
        end;
    end;
run;
Patrick
Opal | Level 21

Good that you asked. Try to avoid SAS macro language until you're sufficiently familiar with SAS Base language. 

Base SAS Arrays should give you what you need.

data want(drop=_:);
  set have;
  array shchecks {*} shcheck1 - shcheck24;
  array sb_displays {*} sb1_display -- sb24_display;

  do _i=1 to dim(shchecks);
    if shchecks[_i] = 1 then
      do;
        TERM =Strip(sb_displays[_i]);
        CAT ='HISTORY';
        output;
      end;
  end;
run;

 

Ideally numbered variables have the number at the end as this makes them easy to reference. 

In above code "sb<n>_display" got the numbering in the middle so here I couldn't just reference as a list.

Assuming that the variables come one after another in the source dataset I've used the double dash notation. You need to check in your source data if my assumption is correct (use a proc contents on the source table with order=varnum; check in the result if the sb<n> variable really come one after the other with consecutive varnums). If the sb<n> vars are not in consecutive order then you need to write them explicitly in the array definition - like:

array sb_displays {*} sb1_display sb2_display sb3_display ....;
Aayushi_17
Quartz | Level 8

thank you this one worked 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 637 views
  • 2 likes
  • 3 in conversation