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

Hi, 

I am a novice to programming world need help.

I have two datasets:

dataset1: (with 620 unique STOREID and with onemore column for var2)

 

STOREIDVar2
1234xxxx
567YYYY
7332aaaa
6664ttt
87cdes
20086aoia
129743uytrewl

 

dataset2: (with 34000 observation and 86 variables including STOREID)

Var1Var2Var3Var4STOREIDVar5Var6Var7Var8
4540a81234f9s97s9kj87683 
    1234087jn9kj876838323
16.9871t81234098ju59kj87683832341
0.0031y8
567
4h7s9kj8768313783232
10982i8
567
9i869kj8769833832359
9874ac8
6664
9juy9kj8768338323
0977cf890a7649kj8761832325
9869fd8906jh9kj87633832132

 

I want to create another sas dataset from dataset2 by selecting all the observations from dataset2 for each of the STOREID present in dataset1. Can anybody help me with the code.(Note: I dont want to merge the given datsets, its more like index-match in excel, taking reference STOREID value from dataset1 and selecting all observations for the same reference STOREID value from dataset2 and creating a new datset with those observations)   

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"Note: I dont want to merge the given datsets," - then you cannot match the data.  Data has to appear in the same place to be evaluated.  Probably the simplest method is:

proc sql;
  create table want as
  select *
  from   ds2
  where storeid in (select storeid from ds1);
quit;

That is however doing a sort of merged table in the background.

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"Note: I dont want to merge the given datsets," - then you cannot match the data.  Data has to appear in the same place to be evaluated.  Probably the simplest method is:

proc sql;
  create table want as
  select *
  from   ds2
  where storeid in (select storeid from ds1);
quit;

That is however doing a sort of merged table in the background.

Ronin
Obsidian | Level 7
Hey Hi, thanks for the solution. it worked. But this just provides me the log file which tells tells the info that table got created with 7095 rows and 86 observation..but din't get me a table with values to see. I want the table as a sas dataset(.sas7bdat) file. Can you help me with this.
Kurt_Bremser
Super User

@Ronin wrote:
Hey Hi, thanks for the solution. it worked. But this just provides me the log file which tells tells the info that table got created with 7095 rows and 86 observation..but din't get me a table with values to see. I want the table as a sas dataset(.sas7bdat) file. Can you help me with this.

If SAS tells you it created something, then it did, period. It may just be that your Enterprise Guide or SAS Studio is not set to automatically open results.

In case of other unclear issues, post the log (use the {i} button so that the log contents are not mangled by the forum software).

 

Regarding your request for code: if you provide example data in usable form (data steps with datalines, see my footnotes for creating such automatically and how to post the code), I can fiddle around with it and give you some examples.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The code I provided has a create table want as statement, which creates a dataset called want in the work library.

Kurt_Bremser
Super User

Probable methods:

  1. join in SQL
  2. merge in a data step
  3. using a hash object in a data step to keep dataset 1 as a reference
  4. create a format from dataset 1 and use that

1 & 2 are the most straight-forward ways, recognizable by any programmer who looks at it

3 & 4 need a little more expertise on the part of the programmer (especially 3), but give you better performance (no sorts needed).

OTOH with 34k and 600 observations, this will be done in fractions of a second anyway.

 

Edit: fixed a typo.

Ronin
Obsidian | Level 7
Thanks for explaining me about all the possible solutions. Can you please help me with any of this code

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2437 views
  • 0 likes
  • 3 in conversation