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

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

ind20
afr15
sa60
na70
eu80
aus50
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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. 

--
Paige Miller
novinosrin
Tourmaline | Level 20

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;
subhani4
Obsidian | Level 7
Could any one help me with call symputx function to perform the task. Thanks in advance!!
novinosrin
Tourmaline | Level 20
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;
Reeza
Super User

@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 : &macroName1.;
%put MacroName19 : &macroName19.;
Kurt_Bremser
Super User

@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.

Reeza
Super User

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.

 

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

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: 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
  • 8 replies
  • 1420 views
  • 8 likes
  • 5 in conversation