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

Hi All,  

 

I am writing a DataStep to pull only certain information out of a file that i imported into SAS..

 

The part i am having trouble with is when i am trying to tell SAS that if data exists in the field (its a character field) that i want it to output to the specifed table.  My code is below.  Can anyone tell me the correct syntax for this datastep.

 

data work.site;
set work.agent2;
if agent_name = "18F-site" then siteflag = 1;
if siteflag then do;
if agent_name is not missing then output work.site; 
if agent_name = "Subtotal of 18F-site" then siteflag = 0;
end;
drop siteflag;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If the site you are looking for is always the first one, then it's easy:

 

data site;

set agent2;

if agent_name='18F-site' then delete;

if agent_name='Subtotal of 18F-site' then stop;

run;

 

If the site you are looking for might be in the middle of the data, it takes a little more programming:

 

data site;

set agent2;

if flag=1 then output;

if agent_name='18F-site' then flag=1;

retain flag;

if agent_name='Subtotal of 18F-site' then stop;

run;

View solution in original post

7 REPLIES 7
Reeza
Super User

If not missing(agent_name) then ...

Astounding
PROC Star

Your intention isn't really clear.  Which observations should go into the new data set?

D_Z_
Obsidian | Level 7

So i have a list of agents from a CSV file that is seperated by site names (18F-Site) and then a subtotal.

 

So i am looking for each dataset to output all the agent names and information between the site name and the subtotal.

 

When i used the code above, i just got the line with the 18F-Site in it but not the names in between which is what my goal was.

 

Dean

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The reason you only get one line out is that siteflag is missing unless assigned on each row, it is not carried over.  You would need to retain this value if you want to use it as a flag like that:

 

data work.site (drop=siteflag);
  set work.agent2;
retain siteflag; if agent_name = "18F-site" then siteflag = 1; if siteflag and not missing(agent_name) then output; if agent_name = "Subtotal of 18F-site" then siteflag = 0; run;

 

 

 

Astounding
PROC Star

If I understand correctly, this might be close to the mark:

 

data site;

set agent2;

if agent_name in ('18F-site', 'Subtotal of 18F-site') then delete;

run;

 

If I misunderstood the proper observations, there must be a fix that is pretty close to this program.

D_Z_
Obsidian | Level 7

That would work... but let me see if i can explain how the data itself is setup. it looks something like this...

 

 

Agent_Name     Field1   Field2   Field3  Field4

18F-Site

John                    100       50        15        10

Jame                    50         40        30       15

Rick                     175       35        25        15

Subtotal 18Fsite   325      125      75         40

 

18F-Site2

Mary                   15          20       30         40

Mike                    20         30        40         50

Subtotal 18FSite2 35       50        70        100

 

18F-Site3

and so on

 

So in essense, i only want the data for 18F-Site and not the others and i don't want to go past the subtotal into the informaiton for my other sites.  I think that may make my intention with this a little clearer and the data step i was using may make a little more sense now.

 

Dean

Astounding
PROC Star

If the site you are looking for is always the first one, then it's easy:

 

data site;

set agent2;

if agent_name='18F-site' then delete;

if agent_name='Subtotal of 18F-site' then stop;

run;

 

If the site you are looking for might be in the middle of the data, it takes a little more programming:

 

data site;

set agent2;

if flag=1 then output;

if agent_name='18F-site' then flag=1;

retain flag;

if agent_name='Subtotal of 18F-site' then stop;

run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 38487 views
  • 0 likes
  • 4 in conversation