Hi guys,
i have two datasets as below.
data temp;
input country $ ;
datalines;
ind
afr
sa
na
eu
aus
;
run;
data test;
input country $ transaction;
datalines;
ind 20
gbr 30
usa 40
aus 50
sa 60
na 70
eu 80
afr 15
zim 17
tx 89
china 88
japan 76
;
run;
i want all the countries from dataset temp copied into macro variable such that my result in dataset want should be like below.
And also please suggest any other possible ways which i can implement to get the result.
country transaction
ind | 20 |
afr | 15 |
sa | 60 |
na | 70 |
eu | 80 |
aus | 50 |
data _null_;
set temp end=lr;
length t $100;
retain t;
t=catx(',',t,quote(strip(country)));
if lr then call symputx('t',t);
run;
%put &t;
data want;
set test;
if country in (&t);
run;
No macros or macro variables are needed. Either PROC SQL or simple data step merge will get you what you want.
UNTESTED CODE BECAUSE I HAVE A MEETING IN 2 MINUTES
data want;
merge temp(in=in1) test;
by country;
if in1;
run;
Advice: macros should be your last choice for manipulating data, used only when you can't get data steps or SQL or SAS PROCs to do what you want; or when you need the code to be dynamic somehow.
data temp;
input country $ ;
datalines;
ind
afr
sa
na
eu
aus
;
run;
data test;
input country $ transaction;
datalines;
ind 20
gbr 30
usa 40
aus 50
sa 60
na 70
eu 80
afr 15
zim 17
tx 89
china 88
japan 76
;
run;
proc sql;
create table want as
select a.*,transaction
from temp a left join test b
on a.country=b.country;
quit;
Use a format, or do a join. No need to involve the macro preprocessor.
data _null_;
set temp end=lr;
length t $100;
retain t;
t=catx(',',t,quote(strip(country)));
if lr then call symputx('t',t);
run;
%put &t;
data want;
set test;
if country in (&t);
run;
@subhani4 wrote:
Could any one help me with call symputx function to perform the task. Thanks in advance!!
As others have indicated this won't work. I have no idea how you'd use those macro variables after the fact. Regardless, here's one option on how to create a list of macro variables easily that will be useful. I strongly recommend against using this and the only use I can conceive of is either homework or a pass-through query, but even then, there are other better methods.
proc sql NOPRINT;
select quote(name) into :name_list separated by ", " from sashelp.class;
quit;
proc sql noprint;
select name into :name1- from sashelp.class;
quit;
%put Name_List: &name_list:
%put Name1 : &name1.;
%put Name2: &name19.;
data _null_;
set sashelp.class;
call symputx(catx("_", 'MacroName', _n_), name);
run;
%put MacroName1 : ¯oName1.;
%put MacroName19 : ¯oName19.;
@subhani4 wrote:
Could any one help me with call symputx function to perform the task. Thanks in advance!!
call symputx is a macro-related subroutine.
I repeat: YOU DO NOT NEED THE MACRO PREPROCESSOR FOR THIS.
Please post your data as a data step in the future and ensure that code is enclosed in code blocks. This makes it easier to copy and paste your data/code.
My apologies, you did do that.
@subhani4 wrote:
Hi guys,
i have two datasets as below.
data temp;
input country $ ;
datalines;
ind
afr
sa
na
eu
aus
;
run;
data test;
input country $ transaction;
datalines;
ind 20
gbr 30
usa 40
aus 50
sa 60
na 70
eu 80
afr 15
zim 17
tx 89
china 88
japan 76
;
run;
i want all the countries from dataset temp copied into macro variable such that my result in dataset want should be like below.
And also please suggest any other possible ways which i can implement to get the result.
country transaction
ind 20 afr 15 sa 60 na 70 eu 80 aus 50
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.