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;
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;
If not missing(agent_name) then ...
Your intention isn't really clear. Which observations should go into the new data set?
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
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;
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.
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.