BookmarkSubscribeRSS Feed
elsfy
Quartz | Level 8

Hi,

Is there a way to select the right dataset based on a condition ?

 

I have many datasets with the same name but their suffix is different and i want to specify a condition just like that : "if dataset name contains XXX then select this dataset and add the variable from this dataset to another dataset " 

 

Thanks

 

6 REPLIES 6
data_null__
Jade | Level 19

I is not clear to me where you need to do this.  In a data step this example might be helpful.

 

44         data all;
45            set data: indsname=dsname;
46            if find(dsname,'02') then putlog 'NOTE: 02 ' dsname=;
47            run;

NOTE: 02 dsname=WORK.DATA02
NOTE: 02 dsname=WORK.DATA02
NOTE: There were 2 observations read from the data set WORK.DATA01.
NOTE: There were 2 observations read from the data set WORK.DATA02.
NOTE: There were 2 observations read from the data set WORK.DATA03.
NOTE: The data set WORK.ALL has 6 observations and 5 variables.
elsfy
Quartz | Level 8
Hi,

Thanks. I replied to another comment being more specific about what i want to do.
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

Have you considered using the dictionary tables in SAS to assign your table to a macro variable which could then be used in subsequent DATA steps or other procedures. This is a simple example about how to use one of those tables.

data test_001;
input x y @@;
cards;
1 2 3 4 5 6 7 8 9 10
;
run;
data test_002;
input x y @@;
cards;
11 12 13 14 15 16 17 18 19 20
;
run;

proc sql;
   select memname into: dsn
   from dictionary.tables
   where substr(memname, 6,3) = '001';
   quit;
/*You can see that &dsn is now a macro variable for the selected table*/
%put &=dsn;
elsfy
Quartz | Level 8

Hey,

 

Thank you. Let me be more specific about my request ;

 

I have a dataset called A like this :

 

ID    year

1      2013

2      2015

...

I need to add for each ID, a variable "quantity". Yet, this variable is in different datasets based on the year date : I have datasets with the year in their name B_2010, B_2011,B_2013,...

These datasets B_year are like this :

 

ID quantity

1  553

2  789

...

I want this in dataset A :

ID    year    quantity

1      2013   553

2      2015   789

...

But how can i tell SAS to search the information in the right table ?

My point is to not merge dataset B_2013, B_2015,... because i have many many tables depending on the year.

Hope it's clear

svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10
In the example, the data set b_year has two values of "quantity" that are merged with two years (2013 and 2015).Does each b_year data set have values from multiple years or one year?
andreas_lds
Jade | Level 19

I would combine all B datasets and add the year, the dataset will then look like your final A dataset. Then you can do something like:

 

data want_a;
  set a;
  if _n_ = 1 then do;
    declare hash h(dataset: "big_b");
    h.defineKey("id", "year");
    h.defineData("quantity");
    h.defineDone();
  end;

  rc = h.find();
run;
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
  • 6 replies
  • 2130 views
  • 2 likes
  • 4 in conversation