Help using Base SAS procedures

Print all Variable names and Values for one record

Reply
Frequent Contributor
Posts: 78

Print all Variable names and Values for one record

Is there a way to print all of the variables names with the value for one individual record?

For example, I would like to see the following output in two columns:

varname response

ID 2
FNAME JOHN
LNAME DOE
DOB 10/10/1910
PHONE 555-555-5555
ZIP 56879

Thanks in advance for your help!
SAS Super FREQ
Posts: 8,869

Re: Print all Variable names and Values for one record

Hi:
Will you only do this for 1 observation or do you want this format for EVERY observation in a group? What is your destination of interest? (RTF, PDF, HTML)? Are DOB, PHONE and ZIP character or numeric variables? SAS expects an entire column of values to be of the same type (so, for example, if you had a column for SALES, then the value for SALES for EVERY row would be numeric).

In your example, FNAME, LNAME and possibly PHONE might be character, but DOB and ZIP might be numeric -- SAS would "want" the second column to be all character -- which would mean that you would have to convert DOB and ZIP to character variables in order to display them in the same column with FNAME, LNAME and PHONE.

Otherwise, the example is sort of like an example in a previous forum posting:
http://support.sas.com/forums/thread.jspa?messageID=46738뚒 (except in that post, all the variables were character.)

cynthia
Frequent Contributor
Posts: 78

Re: Print all Variable names and Values for one record

Posted in reply to Cynthia_sas
So, I only need to do this for one observation. What I really want to do is take a dataset that is in wide format and transpose it to long.

All of the variables are different format types (there are over 3000 variables).

It doesn't matter which destination, anything will work.

If it isn't possible, I guess I can just proc print everything for that one observation.

Thanks!
Regular Contributor
Posts: 171

Re: Print all Variable names and Values for one record

Could you just use PROC TRANSPOSE with a WHERE statement to identify the one observation? Here is an example.

[pre]
DATA HAVE;
INPUT ID $ FNAME:$12. LNAME:$12. DOB:MMDDYY10. PHONE:$12. ZIP $;
FORMAT DOB MMDDYY10.;
DATALINES;
1 JANE DOE 01/01/2001 999-999-9999 12345
2 JOHN DOE 10/10/1910 555-555-5555 56789
3 JIM DOE 12/12/1912 111-111-1111 34567
;;
RUN;

PROC TRANSPOSE DATA=HAVE OUT=WANT(RENAME=(COL1=VALUE)) NAME=VARIABLE;
VAR _ALL_;
WHERE ID = '2';
RUN;

PROC PRINT DATA=WANT NOOBS;
RUN;
[/pre]
Frequent Contributor
Posts: 78

Re: Print all Variable names and Values for one record

Yes, thank you!
SAS Super FREQ
Posts: 8,869

Re: Print all Variable names and Values for one record

Hi:
Did you look at this program:
http://support.sas.com/forums/thread.jspa?messageID=46738뚒

It essentially transposes 4 character variables in a DATA step program and then uses PROC REPORT to create the kind of output you want. The only differences in what you would have to do is:
1) you'd subset your data so you'd only have 1 obs of interest and
2) you'd have to use a DATA step and the PUT function to convert all your numeric variables to character and this means that
3) if you had special user-defined formats, you'd have to take those into account

OR
you'd have to run PROC TRANSPOSE -- this will be easier if you do only have ONE observation. See the example below.

cynthia
[pre]
** alternate approach;
proc sort data=sashelp.class
out=onlyone;
where name in ('Alfred');
by name;
run;

** transpose all variables -- usually only numeric are transposed;
** there will be a note in the log about automatic conversion of;
** numeric to character -- if you do not want this automatic conversion,
** (for example, if you have user-defined formats);
proc transpose data=onlyone out=charout;
var _character_ _numeric_;
run;

proc print data=charout;
run;

proc report data=charout nowd;
column _NAME_ COL1;
define _NAME_ / display 'ID';
define COL1 / display 'Value';
run;
[/pre]
Ask a Question
Discussion stats
  • 5 replies
  • 658 views
  • 0 likes
  • 3 in conversation