BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I'm having a little bit of problem with some mapping ,and I'm hoping someone can help me.

Below are 2 example data sets. what I'd like to do is select columns from data2 that appear as datalines for the variable var in data1 (ie var0, var1, var2) from data2, deleting the rest (in this case var44). So far I've been playing around with Symput, but I can't seem to figure anything out.

Thanks,
Jason

data data1;
length var $ 32;
length var_SB $ 32;
input
var $ var_SB $
;
FORMAT var $32.;
INFORMAT var $32.;
FORMAT var_SB $32.;
INFORMAT var_SB $32.;
datalines;
var0 asfasfg
var1 var1_SB
var2 var2_SB
;
run;
data data2;
input
var0 var1 var2 var44;
datalines;
1 2 3 4
2 4 6 8
1 1 1 1
2 3 4 3
1 1 1 1
;
run;
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
There's no need to involve macro variables. Investigate using either PROC SQL with a JOIN or possibly using a DATA step (against your two files sorted by a common variable BY list) and use a MERGE with a BY list of variables -- and on the MERGE statement you would use the IN= dataset option to detect when one or both files contributed a particular observation during the merge process.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

data step merge site:sas.com

proc sql join site:sas.com
deleted_user
Not applicable
I've spent a couple of hours playing around with this and I've finally come up with a solution, detailed below for anyone who has the same problem. For some reason the do loop had to be inside a macro.

@sbb, I'm not entirely sure where you're coming from because the merge and the proc sql join rely on me having the headings in data1 as var0 var1 etc, rather than in the datalines. Feel free to correct me if I'm not getting the jist.

Cheers
Jason

data _NULL_;
set variables end=last;
call symput ('var'!!left(_N_),compress(var));
call symput ('var_SB'!!left(_N_),compress(var_SB));
if last then call symput ('var_count',_N_);
run;

%macro temp;
data data3;
set data2;
keep %do i=1 %to &var_count;
&&var&i
%end;;;
run;
%mend;
%temp;
data_null__
Jade | Level 19
You can avoid the macro by making a macro variable that is a list of the words in VAR. This will work as long as the list is not longer than the maximum length of a macro variables, about 64k as I recall. Should suffice for most problems.

[pre]
data data1;
infile cards eof=eof;
length var $ 32;
length var_SB $ 32;
length keeplist $128;
retain keeplist;
input var var_SB;
keeplist = catx(' ',keeplist,var);
return;
eof:
call symputx('keeplist',keeplist);
stop;
datalines;
var0 asfasfg
var1 var1_SB
var2 var2_SB
;
run;
%put NOTE: KEEPLIST=&keeplist;

data data2(keep=&keeplist);
input var0 var1 var2 var44;
datalines;
1 2 3 4
2 4 6 8
1 1 1 1
2 3 4 3
1 1 1 1
;
run;
[/pre]
deleted_user
Not applicable
@ data_null_: This is something that I've been doing previously, and it did work. The reason that we're looking to do it directly from a data step is that we need the mapping between var and var_SB for another purpose.

Cheers,
Jason
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
I apologize, having incorrectly read what you wanted to accomplish. Disregard my reply entirely, thanks.

Scott Barry
SBBWorks, Inc.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1027 views
  • 0 likes
  • 3 in conversation