Help using Base SAS procedures

proc append - keep identifier

Reply
Contributor
Posts: 73

proc append - keep identifier

I have a library with data from four years (2010-2014) each dataset includes two variables: id and source

library:

data_2010

data_2011

data_2012

data_2013

I now want to append these years and in the append step (or what step the best for doing this...) create a variable that tells you which year the data comes from. Like this:

id     source     year (new)

1      a             2010     

2      a             2010

1      b            2011

2      b           2011

and so on...

I'd rather not create the variable in each dataset because they are huge

Is it possible to create this variable in the append step?

/Thomas


Super User
Posts: 5,256

Re: proc append - keep identifier

You could create views which contains the years, have proc append read from those instead.

Data never sleeps
SAS Super FREQ
Posts: 683

Re: proc append - keep identifier

Hi bollibompa

See code sample below, it uses some techniques available since SAS9.2 where you can specify data sets lists and the INDSNAME= option on the SET statement.

Bruno

data data_2010 data_2011  data_2012 data_2013;
  do id = 1 to 5;
    source = char(
"ABCDEFG", id);
    output;
 
end;
run;

data data_want;
  set data_2010 - data_2013 indsname=_dsname;
  length year $ 4;
  year = scan(_dsname,
2, "_");
run;
Super User
Super User
Posts: 6,500

Re: proc append - keep identifier

INDSNAME option on the SET statement is what you want.

But you might also want to look into using a BY statement with your SET so that the final dataset will still be sorted by ID.  And if you order the datasets in the SET statement properly then the final dataset will also be sorted by YEAR within ID.

data want ;

  length indsname $41 ;

  set data_2010-data_2013 indsname=indsname ;

  by id source ;

  year = input(scan(idsname,-1,'_'),4.);

run;

Trusted Advisor
Posts: 1,204

Re: proc append - keep identifier

How about this one

proc sql;

insert into all

select id,source, 2010 as source_file

from  data_2010

UNION

select id,source, 2011 as source_file

from  data_2011

UNION

select id,source, 2012 as source_file

from  data_2012

UNION

select id,source, 2013 as source_file

from  data_2013;

quit;

Contributor
Posts: 73

Re: proc append - keep identifier

Thank you all for quick  replies!

INDSNAME was exactly what I was looking for, works perfect!

Thomas

Ask a Question
Discussion stats
  • 5 replies
  • 298 views
  • 0 likes
  • 5 in conversation