BookmarkSubscribeRSS Feed
sijansap
Obsidian | Level 7

Dear SAS users,

 

I have a large dataset with over 500k rows and over 100 variables (columns). However, I am interested particularlly in two columns called ID (identification) and LOC (location) assigned as values as A, B, or C. The ID column include multiple entries of same ID or sometimes single entry.

My question is, how I can create three additional columns say LOC1, LOC2, and LOC 3 of data based on oriiginal LOC. For example, say if the first ID 'A1000' has 6 entries (rows) with three rows of A, one of  B, and two of C in original LOC, now I want only one row of ID 'A1000' but three additional columns (LOC1, LOC2, LOC3) with a value of 3 in LOC1, with a value of 1 in LOC2, and similarly a value of 2 in LOC3.

 

I would appreciate very much for any help.

Thank you in advance.

 

 

6 REPLIES 6
Ksharp
Super User
proc freq data=sashelp.class noprint;
tables sex*age/out=temp list;
run;
proc transpose data=temp out=want;
by sex;
var count;
id age;
run;
sijansap
Obsidian | Level 7

Thanks. I will try.

Reeza
Super User

Do you want to keep the other 100 columns or are you okay dropping them entirely?

 

If you can drop them, look into proc transpose.

 

proc sort data=have;
by id loc;
run;

proc transpose data=have out=want prefix=LOC; by id; var LOC; run;
sijansap
Obsidian | Level 7
Thanks. I will give a try keeping just two columns.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

In addition to the great answers given, you could also do it in a datastep:

data want;
  set have;
  by id loc;
  array loc_{3} 8;
  retain loc_:;
  if first.loc then call missing(of loc_{*});
  select(loc);
    when("A") loc_{1}=sum(loc_{1},1);
    when("B") loc_{2}=sum(loc_{2},2);
    when("C") loc_{3}=sum(loc_{3},3);
    otherwise;
  end;
  if last.id then output;
run;
FreelanceReinh
Jade | Level 19

Hi @sijansap,

 

Here's another data step approach:

data want;
do until(last.id);
  set have(keep=id loc);
  by id;
  array x loc1-loc3;
  i=whichc(loc, 'A', 'B', 'C');
  if i then x{i}=sum(x{i},1);
end;
drop i loc;
run;

 

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
  • 6 replies
  • 1779 views
  • 2 likes
  • 5 in conversation