DATA Step, Macro, Functions and more

How to set first observation as a Variables in SAS

Reply
Contributor
Posts: 46

How to set first observation as a Variables in SAS


Hi All,

I need to drop variables and set First observation as a variables.

Ex:-

VARIABLES : -         VAR1     VAR2     VAR3 

OBSERVATION : -   NAME1  NAME2  NAME3

                                 XXXXX  YYYYY  ZZZZZ

                                AAAAA   BBBBB  CCCCC

OUTPUT LIKE : -   NAME1  NAME2  NAME3

                               XXXXX  YYYYY  ZZZZZ

                                AAAAA   BBBBB  CCCCC

Thanks in advance .........!

Super Contributor
Posts: 305

Re: How to set first observation as a Variables in SAS

Hello,

One solution:


data have;
input (VAR1 VAR2 VAR3 ) ($);
datalines;
NAME1  NAME2  NAME3
XXXXX  YYYYY  ZZZZZ
AAAAA   BBBBB  CCCCC
;

data _null_;
set have (obs=1);
array charall{*} _character_;
length x $ 80;

do i=1 to dim(charall);
x=cat(strip(x),'rename ',vname(charall{i}),'=',strip(charall{i}),';');
end;
call symput('newvar',x);
run;

data want;
&newvar;
set have(firstobs=2);
run;

Super User
Super User
Posts: 7,401

Re: How to set first observation as a Variables in SAS

How are you arriving at your input dataset as it appears like an import data where getnames=no.  I would suggest you address this in your actual import code.  I would recommend not using proc import to start with.  Write a datastep and do the work yourself, gives far more control.  The way you are doing it may lead to problems, for instance do you check to see if the data is a valid SAS name?

Super Contributor
Super Contributor
Posts: 3,174

Re: How to set first observation as a Variables in SAS

Consider investigating the merits with using PROC TRANSPOSE as well as DATA step processing to isolate in a separate SAS dataset/member those observations meaningful to your objective (resulting output dataset variable/column subset).

Scott Barry

SBBWorks, Inc.

Respected Advisor
Posts: 3,777

Re: How to set first observation as a Variables in SAS

12-3-2014 2-20-55 PM.png

data have;
   input (var1-var3) ($);
   cards;
NAME1  NAME2  NAME3
XXXXX  YYYYY  ZZZZZ
AAAAA  BBBBB  CCCCC
;;;;
   run;
proc transpose data=have out=trans1;
   var var:;
   run;
proc print;
  
run;
proc transpose data=trans1 out=trans2(drop=_name_);
   id col1;
   var col2 col3;
   run;
proc print;
  
run;
Ask a Question
Discussion stats
  • 4 replies
  • 279 views
  • 6 likes
  • 5 in conversation