BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pradeepbanu
Obsidian | Level 7

Hello all,

My data looks like 

Source Date Targets count
Email 7-Feb-20 TGT 100
Email 7-Feb-20 NON-TGT 10
Web 8-Feb-20 TGT 50
Web 8-Feb-20 NON-TGT 20
Live 9-Feb-20 TGT 40
Live 9-Feb-20 NON-TGT 5
None 10-Feb-20 TGT 20

 

This what I am doing

 

PROC SORT DATA =DATA_1 OUT = DATA_1_SORT;
BY SOURCE Date Targets;
RUN;

 


DATA DATA_1_SORT;
SET DATA_1_SORT;
BY SOURCE Date Targets;
IF FIRST.Date;

run;

 

Then Transposing

 

options validvarname=any;
PROC TRANSPOSE DATA =DATA_1_SORT PREFIX = _ OUT = TRN_DATA_1_SORT let;
WHERE Targets='TGT';
BY Source;
ID WEEK_END;
VAR COUNT;
RUN;

 

While sorting I am getting the error BY variable are not sorted properly. My output would look like below

 

Source Targets 7-Feb-20 8-Feb-20 9-Feb-20 10-Feb-20
Email TGT 100      
Web TGT   50    
Live TGT     40  
None TGT       20


In this case how will I sort to achieve this transpose data? Error I am getting here is BY variable not sorted properly!!

 

Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Your sample suggests dates are already sorted in source data_1.

So-

data DATA_1;
input Source $	Date :date9.	Targets	$ count;
format date date9.;
cards;
Email	7-Feb-20	TGT	100
Email	7-Feb-20	NON-TGT	10
Web	8-Feb-20	TGT	50
Web	8-Feb-20	NON-TGT	20
Live	9-Feb-20	TGT	40
Live	9-Feb-20	NON-TGT	5
None	10-Feb-20	TGT	20
;

PROC TRANSPOSE DATA =DATA_1 PREFIX = _ OUT = TRN_DATA_1_SORT(drop=_name_) let;
WHERE Targets='TGT';
BY Source notsorted;
VAR COUNT;
id date;
RUN;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Your sample suggests dates are already sorted in source data_1.

So-

data DATA_1;
input Source $	Date :date9.	Targets	$ count;
format date date9.;
cards;
Email	7-Feb-20	TGT	100
Email	7-Feb-20	NON-TGT	10
Web	8-Feb-20	TGT	50
Web	8-Feb-20	NON-TGT	20
Live	9-Feb-20	TGT	40
Live	9-Feb-20	NON-TGT	5
None	10-Feb-20	TGT	20
;

PROC TRANSPOSE DATA =DATA_1 PREFIX = _ OUT = TRN_DATA_1_SORT(drop=_name_) let;
WHERE Targets='TGT';
BY Source notsorted;
VAR COUNT;
id date;
RUN;
ballardw
Super User

Please show the log for everything from your Proc Sort through the Proc transpose code. Copy all of the code, notes, messages and errors from the Log, paste into a code box opened with the </> to preserve any formatting of error messages.

 

Since you have 3 places using the same BY variables it is not clear which one might actually be causing the error.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 386 views
  • 1 like
  • 3 in conversation