BookmarkSubscribeRSS Feed
NagendraBS
Fluorite | Level 6
Hi All,

I have 3 datasets seperately

Data A;
Input Name $
Cards;
ABC
Bcd
Cde
;
Run;

Data B;
Input name $;
Cards;
Wer
Try
Uni
; run;

Data C;
Input name $;
Cards;
Gfh
Hij
Klm
; run;

So I need the output based on condition with macro variable value.
Let say if city = blr then dataset A should append and if city = hyd then dataset B should append to base dataset and if city is blank then dataset C should append to base dataset.

Here is the code I have tried on.

%let city = blr;

Data new;
If city = "blr" then set A end=done;
Else if city = "hyd" the set B end=done;
Else set C end=done;
Run;

Here I see all the records are populating.

Please provide Ur inputs to recreate the code.

Thanks in advance.
5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post an actual example.  Why would you have a macro variable which indirectly points the data to append?  I mean you could do:

%macro appnd ();
  data want;
    set a
  %if "&city."="blr" %then %do;
    b;
  %end;
  %else %do;
    c;
  %end;
  run;
%mend appnd;

%appnd;

But you could vastly simplfy it just by having the macro variable point to the actual table you want to append:

%let city=b;

data want;
  set a &city.;
run;

I suppose the key question here is why your doing this at all.  The data should have all the information needed, i.e. a variable in there for filtering.  So you would simply do:

data want;
  set a b c;
  where city="blr";
run;

I think your just complicating the whole thing.

NagendraBS
Fluorite | Level 6
Hi RW9,

I'll make it simple example based on macro value I need to append the data let say
%let Var = A;

Dataset A should get appended and if the macro variable is blank then Dataset C should append
It is like if u have variable value append that respective dataset and if variable value is blank the by default need to append the dataset C.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Maybe:

data want;
  set %sysfunc(coalescec(&var.,C));
run;

This will set the first if non-missing, otherwise c.  I still don't see any value in this approach however.  macro is not for data processing, put data in the dataset, then use where filtering its quicker, uses less resources, and is simpler coding.

NagendraBS
Fluorite | Level 6
Thanks for your inputs got to solve it
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Cool, please remember to mark appropriate posts as the answer when you have what you need.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 5 replies
  • 2670 views
  • 0 likes
  • 2 in conversation