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

hi,

i have a table (A001) like this:

NAMES CASH
MARIO 100
LUCA 122
MARCO 300

 

how can I store a specific observation (1° row "MARIO" or 2° row "LUCA" or 3° row "MARCO") of the column "NAMES"  of table A001, in the variable FILTER, with  "proc sql"?

 

proc sql  noprint ;
select NAMES into : Filter
from A001
quit;

 

For example, 2° record ( "LUCA" -> FILTER)

 

thank's

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like a silly request.  SQL does not have a concept or order of data.  But SAS does. So you could use dataset options to tell SAS to start reading a dataset at a specific observation number and end at another (equal or greater) observation number.

 

SQL is not really the right tool for that operation. You will have much more flexibility using normal data step code instead.  

data _null_;
 set a001 (firstobs=1 obs=1);
 call symputx('filter',names);
run;

But you can do it in SQL if you must.

select names into :filter trimmed
  from a001(firstobs=1 obs=1)
;

Make sure to use the TRIMMED keyword to prevent the trailing spaces from being stored into the macro variable.

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Do you mean

 

proc sql  noprint ;
select quote(NAMES) into : Filter separated by ','
from A001
quit;
ed_sas_member
Meteorite | Level 14

Hi @Cello23 

It depends on what you want to get:

data A001;
	input NAMES	$ CASH;
	datalines;
MARIO 100
LUCA 122
MARCO 300
;
run;
 
/* &Filter will contain: MARIO LUCA MARCO */

proc sql noprint ;
	select NAMES
	into : Filter separated by " "
	from A001;
quit;

/* &Filter1 will contain MARIO, &Filter2 will contain LUCA, ... */
proc sql noprint ;
	select NAMES
	into : Filter1-:Filter999
	from A001;
quit;
Cello23
Quartz | Level 8

I need to store into variabile FILTER single string, start first observation to last observation (with macro cicle DO WHILE where INDEX are the number of observation table A001):

1° first rows FILTER = 'MARIO"

2° second rows FILTER = 'LUCA'

3°  last row FILTER = 'MARCO'

 

What is the sintax of PROC SQL to store single specific observation (not string) ?

 

THANk'S!!!

Tom
Super User Tom
Super User

Sounds like a silly request.  SQL does not have a concept or order of data.  But SAS does. So you could use dataset options to tell SAS to start reading a dataset at a specific observation number and end at another (equal or greater) observation number.

 

SQL is not really the right tool for that operation. You will have much more flexibility using normal data step code instead.  

data _null_;
 set a001 (firstobs=1 obs=1);
 call symputx('filter',names);
run;

But you can do it in SQL if you must.

select names into :filter trimmed
  from a001(firstobs=1 obs=1)
;

Make sure to use the TRIMMED keyword to prevent the trailing spaces from being stored into the macro variable.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2226 views
  • 2 likes
  • 4 in conversation