SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

Hi,

I try to understand the logic.

If without separated by, why SAS only return first value, at least it can return all values without space

Thank you,

HHCFX

 

data have;
input abIc ID1 bxI4D b_ID2 xyzID_3; 
cards;
1 2 3 4 5
;run;

proc contents data=have out=varname;run;

proc sql;
select name into :droplist separated by ' ' from varname  where index(name,'ID')>0; quit;
%put &droplist;

*VS without separated by ' ';

proc sql;
select name into :droplist /*separated by ' '*/ from varname  where index(name,'ID')>0; quit;
%put &droplist;
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

The "separated by ..." tells sql that you wants a list of names.

The list can be separated by any character given in a single quotes.

Otherwise the names are overwritten and you probably get the last one only.

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

The "separated by ..." tells sql that you wants a list of names.

The list can be separated by any character given in a single quotes.

Otherwise the names are overwritten and you probably get the last one only.

hhchenfx
Rhodochrosite | Level 12

Thanks for your explanation.

It makes sense now.

HHC

Patrick
Opal | Level 21

@Shmuel wrote:

The "separated by ..." tells sql that you wants a list of names.

The list can be separated by any character given in a single quotes.

Otherwise the names are overwritten and you probably get the last one only.


You get the value of the first row which is the smart thing as this way SAS doesn't have to process the whole table.

I've just tested for two tables - one with only a few rows and one with 20M rows. SQL ... INTO :name run in no time for both tables

NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
Shmuel
Garnet | Level 18

@Patrick , to your note

You get the value of the first row which is the smart thing as this way SAS doesn't have to process the whole table.

I've just tested for two tables - one with only a few rows and one with 20M rows. SQL ... INTO :name run in no time for both tables

selecting NAME by SQL - selects variable names from dictionary, no matter how many rows are in the dataset. 

Patrick
Opal | Level 21

@hhchenfx 

That's just how it's implemented and the syntax works.

 

Alternatively to a list of values you can also create individual macro variables per row - so you've got all the options at your fingertips.

proc sql noprint;
  select name into :namelist separated by ''
  from sashelp.class
  ;
quit;
%put &=namelist;

proc sql noprint;
  select name into :m_name_1 - :m_name_9999
  from sashelp.class
  ;
quit;

%macro printMvars();
  %do i=1 %to &sqlobs;
    %put m_name_&i= &&m_name_&i;
  %end;
%mend;
%printMvars()

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 15310 views
  • 2 likes
  • 3 in conversation