BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jhh197
Pyrite | Level 9

I have a dataset which looks like below . 

DatePRICE_DSLoc_typ
2013016525 MN SKP
2013036525 MN SKP
2013106525 MN SKP
2013126525 MN SKP
2014016525 MN SKP
2014026525 MN SKP
20130179.25SP NC KPP
20130379.25SP NC KPP
20131079.25SP NC KPP
20131279.25SP NC KPP
20140179.25SP NC KPP
20140279.25SP NC KPP

But when i transpose data i want output names which should look like below 

proc transpose data= out=(drop=_name_ _label_) prefix=column;

by loc_typ;

var price_ds;

run;

But i want all variables to be named like below is that possible ? Can anyone please help ? 

 

Column1Column2Column3Column4Column5Column6Column7
25 MN SKP656565656565
SP NC KPP79.2579.2579.2579.2579.2579.25
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Read you log or post your log. Your Proc Transpose code throws an error:

 

202  proc transpose data=have  out=(drop=_name_ _label_) prefix=column;
                                   -
                                   22
                                   76
ERROR 22-322: Expecting a name.
ERROR 76-322: Syntax error, statement will be ignored.
203  by loc_typ;
204  var price_ds;
205  run;

OUT= requires a data set name as the first thing.

 

You aren't doing anything with variable labels (at least as shown) so _label_ in the drop isn't needed.

 

Please provide data in the form of working data step code so we don't have to deal with artifacts from odd pictures or "table" layouts.

You want to rename your Loc_typ variable to column1 before use. Adding a variable for an ID statement will work with the prefix to generate the desired column labels.

data have;
 infile datalines dlm=',';
 input Date	PRICE_DS	Loc_typ $10.;
datalines;
201301,65,25 MN SKP
201303,65,25 MN SKP
201310,65,25 MN SKP
201312,65,25 MN SKP
201401,65,25 MN SKP
201402,65,25 MN SKP
201301,79.25,SP NC KPP
201303,79.25,SP NC KPP
201310,79.25,SP NC KPP
201312,79.25,SP NC KPP
201401,79.25,SP NC KPP
201402,79.25,SP NC KPP
;

data need;
   set have;
   by loc_typ;
   if first.loc_typ then row=2;
   else row+1;
   rename loc_typ=column1;
run;
proc transpose data=need
     out=want (drop=_name_ )
         prefix=column;
by column1;
id row;
var price_ds;
run;

If you just rename the By variable to column1 then you get a conflict with the output data and lose the first transposed column because you "stole" the column name.

 

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ
data have;
infile cards delimiter='|';
input Date $ PRICE_DS Loc_typ $;
cards;
201301 |  65      | 25 MN SKP
201303 |  65      | 25 MN SKP
201310 |  65      | 25 MN SKP
201312 |  65      | 25 MN SKP
201401 |  65      | 25 MN SKP
201402 |  65      | 25 MN SKP
201301 |  79.25   | SP NC KPP
201303 |  79.25   | SP NC KPP
201310 |  79.25   | SP NC KPP
201312 |  79.25   | SP NC KPP
201401 |  79.25   | SP NC KPP
201402 |  79.25   | SP NC KPP
;
run;

proc transpose data=have out=want(drop=_name_ _label_) prefix=column;
 by loc_typ;
 var price_ds;
run;

proc datasets library=work NoList NoDetails memtype=DATA;
 modify want;
  rename column6 = column7;
  rename column5 = column6;        
  rename column4 = column5;
  rename column3 = column4;
  rename column2 = column3;
  rename column1 = column2;
  rename Loc_typ = column1;
run;
QUIT;
/* end of program */

Koen

 
jhh197
Pyrite | Level 9

Thank you so much for the reply . Is there any other way if i have 40 columns i need to rename them manually right what if new date in database then i need to update code manually . Is there any automatic way ?

sbxkoenk
SAS Super FREQ
data have;
infile cards delimiter='|';
input Date $ PRICE_DS Loc_typ $;
cards;
201301 |  65      | 25 MN SKP
201303 |  65      | 25 MN SKP
201310 |  65      | 25 MN SKP
201312 |  65      | 25 MN SKP
201401 |  65      | 25 MN SKP
201402 |  65      | 25 MN SKP
201301 |  79.25   | SP NC KPP
201303 |  79.25   | SP NC KPP
201310 |  79.25   | SP NC KPP
201312 |  79.25   | SP NC KPP
201401 |  79.25   | SP NC KPP
201402 |  79.25   | SP NC KPP
;
run;

proc transpose data=have out=want(drop=_name_ _label_) prefix=column;
 by loc_typ;
 var price_ds;
run;

proc contents data=want out=outputds;   run;
proc sort     data=outputds; by varnum; run;

data _null_; 
 set outputds end=last;
 if last then do; call symputx("maximum",varnum); end;
run;
%PUT &=maximum;

%MACRO ABCXYZ;
proc datasets library=work NoList NoDetails memtype=DATA;
 modify want;
%DO i=&maximum. %TO 2 %BY -1;
  rename column%eval(&i.-1) = column&i.;
%END;
  rename Loc_typ = column1;
run;
QUIT;
%MEND  ABCXYZ;

options mprint;
%ABCXYZ
QUIT;
/* end of program */

Koen

 
ballardw
Super User

Read you log or post your log. Your Proc Transpose code throws an error:

 

202  proc transpose data=have  out=(drop=_name_ _label_) prefix=column;
                                   -
                                   22
                                   76
ERROR 22-322: Expecting a name.
ERROR 76-322: Syntax error, statement will be ignored.
203  by loc_typ;
204  var price_ds;
205  run;

OUT= requires a data set name as the first thing.

 

You aren't doing anything with variable labels (at least as shown) so _label_ in the drop isn't needed.

 

Please provide data in the form of working data step code so we don't have to deal with artifacts from odd pictures or "table" layouts.

You want to rename your Loc_typ variable to column1 before use. Adding a variable for an ID statement will work with the prefix to generate the desired column labels.

data have;
 infile datalines dlm=',';
 input Date	PRICE_DS	Loc_typ $10.;
datalines;
201301,65,25 MN SKP
201303,65,25 MN SKP
201310,65,25 MN SKP
201312,65,25 MN SKP
201401,65,25 MN SKP
201402,65,25 MN SKP
201301,79.25,SP NC KPP
201303,79.25,SP NC KPP
201310,79.25,SP NC KPP
201312,79.25,SP NC KPP
201401,79.25,SP NC KPP
201402,79.25,SP NC KPP
;

data need;
   set have;
   by loc_typ;
   if first.loc_typ then row=2;
   else row+1;
   rename loc_typ=column1;
run;
proc transpose data=need
     out=want (drop=_name_ )
         prefix=column;
by column1;
id row;
var price_ds;
run;

If you just rename the By variable to column1 then you get a conflict with the output data and lose the first transposed column because you "stole" the column name.

 

jhh197
Pyrite | Level 9

thank you so much for all the help 

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
  • 5 replies
  • 337 views
  • 3 likes
  • 3 in conversation