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

I am trying below code-

Columns in data(new) are in the following format- A, B, E, F, C, D.

Data new;
input A B E F C D;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;

 

but while getting contents the sequence is changing.

i.e. data(new1) is consisting columns of of data(new) in the following format- A, B, C, D, E, F. 

proc print data=new;run;
proc contents data=new out=new1(keep=NAME) noprint;
run;
proc print data=new1;run;

Question- Why it is changing the sequence?

If I required data(new1) in the same format then what should I do? 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please avoid bad coding practices such as missing run off and putting run on same line, make code as easy to read as possible.  

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc contents data=new order=varnum out=new1(keep=Name);
run;

proc print data=new1;
run;

Results in:

...


                                    Variables in Creation Order
 
                                   #    Variable    Type    Len

                                   1    a           Num       8
                                   2    b           Num       8
                                   3    e           Num       8
                                   4    f           Num       8
                                   5    c           Num       8
                                   6    d           Num       8
                                          The SAS System        11:10 Monday, January 28, 2019   2

                                           Obs    NAME

                                            1      a  
                                            2      b  
                                            3      c  
                                            4      d  
                                            5      e  
                                            6      f  

In section 1 you will see the variables ordered as per the creation order.

If you want the print to display in that order, then you need to sort the dataset:

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc contents data=new out=new1 (keep=varnum name) noprint;
run;

proc sort data=new1;
  by varnum;
run;

proc print data=new1;
  var name;
run;

 

And you can ditch the proc compare totally and just report out the metadata like:

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc sort data=sashelp.vcolumn out=new1 (keep=name);
  by varnum;
  where libname="WORK" and memname="NEW";
run;

proc print data=new1;
  var name;
run;

Thats generally more effective than proc contents then report. 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you look at sashelp.vcolumn - which is basically what you are printing, you will see the column name, label, length etc. as metadata.  One of the columns in there is varnum.  This defines the actual order, but the data is sorted alphabetically by default.  So you can just sort by using the orderby option:

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#p0b3vc5qd4rg06n1g6ds4...

 

Kaushik1
Obsidian | Level 7

Tried following-

proc contents data=new order=varnum out=new1(keep=Name);
run;
proc print data=new1;run;

Not getting sequence in original form in data(new1).

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please avoid bad coding practices such as missing run off and putting run on same line, make code as easy to read as possible.  

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc contents data=new order=varnum out=new1(keep=Name);
run;

proc print data=new1;
run;

Results in:

...


                                    Variables in Creation Order
 
                                   #    Variable    Type    Len

                                   1    a           Num       8
                                   2    b           Num       8
                                   3    e           Num       8
                                   4    f           Num       8
                                   5    c           Num       8
                                   6    d           Num       8
                                          The SAS System        11:10 Monday, January 28, 2019   2

                                           Obs    NAME

                                            1      a  
                                            2      b  
                                            3      c  
                                            4      d  
                                            5      e  
                                            6      f  

In section 1 you will see the variables ordered as per the creation order.

If you want the print to display in that order, then you need to sort the dataset:

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc contents data=new out=new1 (keep=varnum name) noprint;
run;

proc sort data=new1;
  by varnum;
run;

proc print data=new1;
  var name;
run;

 

And you can ditch the proc compare totally and just report out the metadata like:

data new;
  input a b e f c d;
datalines;
1 2 3 4 6 5
2 4 6 7 8 6
1 3 4 5 3 7
4 6 2 1 3 7
1 4 9 7 2 3
;
run;

proc sort data=sashelp.vcolumn out=new1 (keep=name);
  by varnum;
  where libname="WORK" and memname="NEW";
run;

proc print data=new1;
  var name;
run;

Thats generally more effective than proc contents then report. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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