BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ANLYNG
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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.

ANLYNG
Pyrite | Level 9
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
Kurt_Bremser
Super User

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?

ANLYNG
Pyrite | Level 9

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

Astounding
PROC Star

Aren't you getting an error message from this:

 

set test2;

 

There is no data set named test2.

ANLYNG
Pyrite | Level 9

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

 

I read 300 observation in but no out.

 

 

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ANLYNG
Pyrite | Level 9

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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