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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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