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

Hello,

 

I am trying to work with a sample data about people's visit to a certain state and sort out whether each person has visited 

 

the capital (Washington D.C ) or not and how many times each person visited there by flight.

 

The major problem I am having is grouping with the new variables. 

 

Below is my sample data .

 

ID= personal identification number 

 

FLIGHT_TICKET_N= flight ticket number

 

STATE = state in the U.S (DC=Washington D.C, captial city of the U.S) 

 

data VISIT;
input ID$ FLIGHT_TICKET_N STATE$;
cards;
10063 1111 DC
10063 1112 DC
10063 1113 DC
10073 2221 DC
10073 2223 TX
10083 3331 CA
10083 3332 WA
run;

 


data visit;
set visit;
captial=".";
if substr(STATE,1,2) in ("DC") then captial=1;
if captial ~= 1 then captial=0;
run;

 

data visit_1;
set visit;
by ID;
if first.ID then acc_capital=0;
acc_capital + captial;
run;

 

so far it works as I intended. However, when I tried this: 


proc sort data=visit_1 out=visit_2 nodupkey; by ID; run;

 

The results do not correctly reflect:

 

#1. whether each person has visited the capital city or not

 

#2. the accumulated number of visit to the capital city for each person.

 

as shown in the picture. 

 

I really need to figure this out.

 

I am I would appreciate any help.

 

Thank you in advance. 


sasq1.jpg
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Sort with nodupkey only removes any records with duplicate keys after encountering the first on, and does not help in accumulating values.

Other procedures are there for this:

data VISIT;
input ID$ FLIGHT_TICKET_N STATE$;
cards;
10063 1111 DC
10063 1112 DC
10063 1113 DC
10073 2221 DC
10073 2223 TX
10083 3331 CA
10083 3332 WA
;
run;

data visit;
set visit;
if substr(STATE,1,2) in ("DC")
then capital = 1;
else capital = 0;
run;

proc summary data=visit;
by id;
var capital;
output
  out=want (drop=_type_ _freq_)
  max(capital) = capital
  sum(capital) = acc_capital
;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Sort with nodupkey only removes any records with duplicate keys after encountering the first on, and does not help in accumulating values.

Other procedures are there for this:

data VISIT;
input ID$ FLIGHT_TICKET_N STATE$;
cards;
10063 1111 DC
10063 1112 DC
10063 1113 DC
10073 2221 DC
10073 2223 TX
10083 3331 CA
10083 3332 WA
;
run;

data visit;
set visit;
if substr(STATE,1,2) in ("DC")
then capital = 1;
else capital = 0;
run;

proc summary data=visit;
by id;
var capital;
output
  out=want (drop=_type_ _freq_)
  max(capital) = capital
  sum(capital) = acc_capital
;
run;
sasworker16
Calcite | Level 5

Dear KurtBremser,

 

Thank you very much for your help and support.

 

 

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
  • 935 views
  • 0 likes
  • 2 in conversation