DATA Step, Macro, Functions and more

Dynamically create datasets

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Dynamically create datasets

I need to dynamically to create location datasets with 100 observations for each location.The idea is that they should be updated locally by editing.

 

I first create a list (macro called name1.) with the names of the files. Then create the design of the table.

 

I can not get it to work in the last part where I need to filter the observations onlovation i.e. I need the right and only 100 observations in each dataset.

 

Hope you can help.

 

Here is some code:

 

 

proc sql noprint;

select namestrip

into :name1 separated by ' '

 

from location;

quit;

run;

 

data test;

set Location(keep=XXX YYY);

length Description $250.;

do i=1 to 100;

 

Label XXX="XXX" ;

Label YYY="YYY";

Description=" ";

run;

 

 

data  GGG.&name1.;

set test2;

 

if &name1.=namestrip then output;

run;


Accepted Solutions
Solution
‎11-14-2016 01:37 PM
Trusted Advisor
Posts: 1,022

Re: Dynamically create datasets

You want a program that looks like:

 

data  loc1 (where=(namestrip='LOC1')  loc2 (where=(namestrip='LOC2'))  ....  

   set have;

 run;

 

This program does that:

 

Notes:

  1. Instead of select distinct namestrip,  you can "select distinct" expression-using-namestrip to generate the arguments of the DATA statement above.  Look at the value of the DSLIST macrovar created in the program.
  2. This does require two passes of the data - once to determine DSLIST, and once to wrtie the data sets.

 

Regards,

Mark

 


data have;
  input namestrip $4. XXX YYY;
datalines;
loc1 1 1
loc1 1 1
loc2 3 4
loc2 7 7
loc3 3 4
loc3 7 7
run;

proc sql noprint;
  select distinct 
     cats(namestrip
         , '(where=(namestrip='
         ,quote(trim(namestrip))
         ,'))'
         )
   into :dslist separated by ' ' 
   from have;
quit;

%put &=dslist;

data &dslist ;
  set have;
run;

View solution in original post


All Replies
Super User
Posts: 7,844

Re: Dynamically create datasets

You need to put &name1. into double quotes, so it will be recognized by the data step compiler as a string literal and not a variable name.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 75

Re: Dynamically create datasets

Posted in reply to KurtBremser
when I so I get zero observations in the output. the macro contains a list loc1 loc2 loc2. Some how I need to test each value toward namestrip. Any ideas? Thanks in advance.


My data is:
namestrip XXX YYY
loc1 1 1
loc1 1 1
....
loc2 3 4
loc2 7 7
.......
loc3 3 4
loc3 7 7



The data is


locatio
Super User
Posts: 7,844

Re: Dynamically create datasets

Please stay with your original problem. Once that is solved, you will either find a solution for similar problems yourself, or we can then deal with the new question.

Have you already tried my suggestion?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 75

Re: Dynamically create datasets

Posted in reply to KurtBremser

I have tried your suggestion. It comes near a solution but I get no observations out.

Super User
Posts: 5,516

Re: Dynamically create datasets

Aren't you getting an error message from this:

 

set test2;

 

There is no data set named test2.

Frequent Contributor
Posts: 75

Re: Dynamically create datasets

Posted in reply to Astounding

I tried to make it simple-Imean to set test.....

 

I read 300 observation in but no out.

 

 

Solution
‎11-14-2016 01:37 PM
Trusted Advisor
Posts: 1,022

Re: Dynamically create datasets

You want a program that looks like:

 

data  loc1 (where=(namestrip='LOC1')  loc2 (where=(namestrip='LOC2'))  ....  

   set have;

 run;

 

This program does that:

 

Notes:

  1. Instead of select distinct namestrip,  you can "select distinct" expression-using-namestrip to generate the arguments of the DATA statement above.  Look at the value of the DSLIST macrovar created in the program.
  2. This does require two passes of the data - once to determine DSLIST, and once to wrtie the data sets.

 

Regards,

Mark

 


data have;
  input namestrip $4. XXX YYY;
datalines;
loc1 1 1
loc1 1 1
loc2 3 4
loc2 7 7
loc3 3 4
loc3 7 7
run;

proc sql noprint;
  select distinct 
     cats(namestrip
         , '(where=(namestrip='
         ,quote(trim(namestrip))
         ,'))'
         )
   into :dslist separated by ' ' 
   from have;
quit;

%put &=dslist;

data &dslist ;
  set have;
run;
Frequent Contributor
Posts: 75

Re: Dynamically create datasets

This is a really nice solution and it works perfectlly.Thanks a lot.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 242 views
  • 0 likes
  • 4 in conversation