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
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.
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;
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
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.