Help using Base SAS procedures

Mapping datalines as column headers

Reply
N/A
Posts: 0

Mapping datalines as column headers

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;
Super Contributor
Super Contributor
Posts: 3,174

Re: Mapping datalines as column headers

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
N/A
Posts: 0

Re: Mapping datalines as column headers

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;
Respected Advisor
Posts: 3,777

Re: Mapping datalines as column headers

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]
N/A
Posts: 0

Re: Mapping datalines as column headers

@ 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
Super Contributor
Super Contributor
Posts: 3,174

Re: Mapping datalines as column headers

I apologize, having incorrectly read what you wanted to accomplish. Disregard my reply entirely, thanks.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 5 replies
  • 105 views
  • 0 likes
  • 3 in conversation