BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8
data _null_;
    set sashelp.class end=last ;
    /* Check if the observation is the 1st, 15th, 6th, or the last observation */
    if _n_ in (1, 15, 6) or last then do;
        /* Use PUT with column positioning correctly */
        put @15 name $10. @30 sex $1. @40 age 3. @50 height 5.1 @60 weight 5.1 @70 ;
    end;
run;

Hi Experts,

In the above code i want get variable names also  in log window with data when using _NULL_ dataset 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming you want to PUT the names (not retrieve them from some unspecified place) just use a PUT statement with quoted strings.  You probably only want to write the names once say on the first observation.

data _null_;
    set sashelp.class end=last ;
    if _n_=1 then put @15 'Name' @(30-2) 'Sex' @40 'Age' @50 'Height' @60 'Weight' ;
    /* Check if the observation is the 1st, 15th, 6th, or the last observation */
    if _n_ in (1, 15, 6) or last then do;
        /* Use PUT with column positioning correctly */
        put @15 name $10. @30 sex $1. @40 age 3. @50 height 5.1 @60 weight 5.1 @70 ;
    end;
run;

Result

              Name         Sex         Age      Height    Weight
              Alfred         M          14        69.0     112.5
              James          M          12        57.3      83.0
              Philip         M          16        72.0     150.0
              William        M          15        66.5     112.0

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

I don't understand. Variable names in the log window? Doesn't the log show you the code you execute, which includes the PUT statement with the variable names?

--
Paige Miller
pavank
Quartz | Level 8

Hi @Kurt_Bremser ,

 I want to display top  as below

Name       sex     age  height      weight

Alfred        M      14       69             112.5 
James       M       12       57.3           83 6
Philip         M      16       72             150 

William     M      15       66.5         112 19

 

Kurt_Bremser
Super User

Use PROC PRINT and you get it in the Output window.

If you want to see the names in the log, use this as the first statement in the step.

if _n_ = 1 then put "name sex age height weight";

(adjust positions as needed)

pavank
Quartz | Level 8

Hi @Kurt_Bremser 

Thank you very much for your solution

PaigeMiller
Diamond | Level 26

@pavank 

 

Please listen to @Kurt_Bremser and use PROC PRINT, this is sooooo much easier than writing DATA _NULL_ code to do what you want. And PROC PRINT has many other useful option as well, that you may need on other problems.

 

To steal again from Kurt, see Maxim 7

Maxim 7

There is a procedure for it.
(The exception proves the rule)

Learn to use prefabricated procedures for solving your tasks. 5 lines of proc means may equal 20 lines (or more) of data step logic.

--
Paige Miller
Tom
Super User Tom
Super User

Assuming you want to PUT the names (not retrieve them from some unspecified place) just use a PUT statement with quoted strings.  You probably only want to write the names once say on the first observation.

data _null_;
    set sashelp.class end=last ;
    if _n_=1 then put @15 'Name' @(30-2) 'Sex' @40 'Age' @50 'Height' @60 'Weight' ;
    /* Check if the observation is the 1st, 15th, 6th, or the last observation */
    if _n_ in (1, 15, 6) or last then do;
        /* Use PUT with column positioning correctly */
        put @15 name $10. @30 sex $1. @40 age 3. @50 height 5.1 @60 weight 5.1 @70 ;
    end;
run;

Result

              Name         Sex         Age      Height    Weight
              Alfred         M          14        69.0     112.5
              James          M          12        57.3      83.0
              Philip         M          16        72.0     150.0
              William        M          15        66.5     112.0

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 7 replies
  • 619 views
  • 0 likes
  • 4 in conversation