BookmarkSubscribeRSS Feed
pmpradhan
Quartz | Level 8

Currently, My data is like:

ABCD
NameSexAddressPhone
RachelF789 Mid County111-111-1111

 

My first observation is my variable name. therefore, I want my data to look like:

 

NameSexAddressPhone
RachelF789 Mid County111-111-1111

 

Please help!

10 REPLIES 10
novinosrin
Tourmaline | Level 20

Is current sas dataset?

excel or some other like csv?

pmpradhan
Quartz | Level 8

Yes it is SAS dataset.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

Sorry to say that is a poor data table.  Looks like the solution should have been handled during the creating of the original table.  Why would anyone create a table in that manner in SAS.

pmpradhan
Quartz | Level 8
Agree, One of the simple way, I found is renaming all vars then deleting first observation. I was hoping to see if others find a different way.
novinosrin
Tourmaline | Level 20

Well done, i think your approach is pretty sound!

mkeintz
PROC Star

So you have a sas dataset with (1) var names of A, B, C, ..., (2) the intended var names are actually the values of A, B, C, ... in the first row.  Looks like it was once a spreadsheet.

 

Is the spreadsheet still available?  If so, use proc import.

 

 

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
pmpradhan
Quartz | Level 8
Unfortunately not.
mkeintz
PROC Star
  1. Establish a temporary file for holding renames
  2. Read the first obs
  3. Write to #1 above using a "put (_all_) (=)" statement to write   a set of name=value pairs, where the names will be A, B, C, D and the values will be the content in the first obs.
  4. Run a second data step with (firstobs=2),  and include the renames written above
data have;
  input A $6. +1 B $3. +1 C $14.  +1  D $12.;
datalines;
Name   Sex Address        Phone 
Rachel F   789 Mid County 111-111-1111 
run;

filename renames temp ;

data _null_;
  set have (obs=1);
  file renames;
  put 'rename '
    / (_all_) (=)
	/ ';'
	;
run;

data want;
  set have (firstobs=2);
  %include renames ;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
tocilj
Calcite | Level 5

Outdated reply but may help someone in future

 

/*----------------------------------------*/
data test;
infile datalines;
input A : $10. B : $3. C & $20. D : $20.;
datalines;
Name Sex Address Phone
Rachel F 789 Mid County 111-111-1111
;
run;

proc transpose data=test out=testt;
var A B C D;
run;

proc transpose data=testt out=testtt(drop=_:);
var col2;
id col1;
run;
/*----------------------------------------*/

 

mkeintz
PROC Star

@tocilj 

 

The double proc transpose is nice in that it has a simple structure, and it uses a pre-packaged sas routine.

 

But it may not scale well.  Transposing 1,000,000 observations will create a dataset with 1,000,000 variables in the intermediate data set TEST1.  The entire dataset would have to be processed two times, which can begin to use significant resources.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 3984 views
  • 2 likes
  • 5 in conversation