The data I am working with is a very large dataset: the MIT Atlas data recording product trade by origin and destination country by year by product. I have combined it with CEPII data to determine how far apart each two countries are. My issue is this: I can successfully sum up exports by country to a given country, but my program does not do this for every country. The state of my current code gives me the sum of each country's exports to and imports from Zimbabwe, and stops there.
What I need: the exports to and imports from each country A and country B.
My current code:
data sasuser.want; set sasuser.have; by origin destination year; if First.destination then TotalExports = 0; TotalExports +exports; if Last.destination; if First.origin then TotalImports = 0; TotalImports + imports; if Last.origin; TotalTrade = TotalExports +TotalImports; run;
What's going wrong here? Any help would be greatly appreciated.
I should note that I have successfully grabbed the data I want creating 2 separate datasets and then merging them. I am curious at this point about the feasability of doing this in a more concise manner.
This is much easier to solve if you can provide sample data - fake is fine. If you make the example data and show the expected output you’ll definitely get a solution quickly.
How big is your dataset? SAS is designed to handle large data - no possibly no issues there.
And any particular reason you aren’t using PROC MEANS or FREQ to calculate summary stats. Seems like it would be easier.
The middle section of this presentation goes over PROC MEANS
@cdunkleychan wrote:
The data I am working with is a very large dataset: the MIT Atlas data recording product trade by origin and destination country by year by product. I have combined it with CEPII data to determine how far apart each two countries are. My issue is this: I can successfully sum up exports by country to a given country, but my program does not do this for every country. The state of my current code gives me the sum of each country's exports to and imports from Zimbabwe, and stops there.
What I need: the exports to and imports from each country A and country B.
My current code:
data sasuser.want; set sasuser.have; by origin destination year; if First.destination then TotalExports = 0; TotalExports +exports; if Last.destination; if First.origin then TotalImports = 0; TotalImports + imports; if Last.origin; TotalTrade = TotalExports +TotalImports; run;What's going wrong here? Any help would be greatly appreciated.
I should note that I have successfully grabbed the data I want creating 2 separate datasets and then merging them. I am curious at this point about the feasability of doing this in a more concise manner.
This slide deck doesn't really help me understand how to use indexing, but I see how it could be useful. It would be really hard to include my data here (143m rows). Below is a screenshot from SAS of what it looks like. I tried toying around with the other suggestion; I made no headway/
Pictures of your data are not really helpful.
But there is no need to post your REAL data.
Just post something that can be use to describe your question. Post example input and the desired output for that input. Include just enough complexity to demonstrate the issue. You should post it in the form of a data step that others can run to create the problem.
Or ask the question using one the sample datasets from SASHELP libref. Like SASHELP.CLASS or SASHELP.CARS.
You will probably figure out the answer yourself just from going through the steps of fully explaining what you are trying to do.
@cdunkleychan wrote:
This slide deck doesn't really help me understand how to use indexing, but I see how it could be useful.
The middle section of this presentation goes over PROC MEANS
Sounds like your question is this:
What I need: the exports to and imports from each country A and country B.
So you want one dataset with ORIGIN, YEAR and TOTAL_EXPORTS and other with DESTINATION,YEAR and TOTAL_IMPORTS?
Does your input data have MPORT and EXPORT as two different fields?
Does your input data have both exchange directions listed ( A->B and B->A) as separate observations? or do you need to impute that exports from A to B are also imports to B from A?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.