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.
Geo-
Quartz | Level 8

I am use select into creating a macro array

 

proc sql;

select numValue into:num_value separated by ' ' from tableA;

quit;

 

%put %scan(num_value,1);

 

 

however,the value in macro num_value  did not arrange their numeric values from its original order(from small to large).

so how could I arrage their values descending or ascending depending on their index,or the macro array has a same order as the original table is.

 

thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

By default, a decimal point is a delimiter for %SCAN.  So you have to specify that a blank is the only delimiter:

 

%put %scan(&start_num, 1, %str( ));

 

Also, make sure you have a blank in quotes:  separated by ' '

 

If you omit the blank, SQL will use 0 characters as the delimiter:  separated by ''

 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

SQL doesn't promise the order of anything, except when you use an ORDER BY clause.  And that doesn't apply to macro variables.

 

You do have the option (if it is appropriate) of adding DISTINCT:

 

select distinct numvalue into : ..........

 

DISTINCT forces the order to be ascending.  However, it also removes duplicates which may not be what you want.

Geo-
Quartz | Level 8

DISTINCT forces the order,and return

start
25.5
33.5
42.5
54.5
98

 

but when I am using %put %scan(&start_num,1);

it gave me :25

 

%put %scan(&start_num,2);

return me:533

 

why and how to fix it?

Astounding
PROC Star

By default, a decimal point is a delimiter for %SCAN.  So you have to specify that a blank is the only delimiter:

 

%put %scan(&start_num, 1, %str( ));

 

Also, make sure you have a blank in quotes:  separated by ' '

 

If you omit the blank, SQL will use 0 characters as the delimiter:  separated by ''

 

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
  • 3 replies
  • 3685 views
  • 0 likes
  • 2 in conversation