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

 

I am having something like in sql;

 

Campaignname is a string, APPLICATIONCAMPAIGNS_SRGT_ID is a numeric

 

 

 

 

 

SELECT DISTINCT [CampaignName]
,COUNT(cAMPAIGNNAME) AS NBROFCAMPAIGNS
,ROW_NUMBER() OVER(Order by min(APPLICATIONCAMPAIGNS_SRGT_ID)) AS CampaignNumber
FROM  Applicationcampaigns
GROUP BY CampaignName
ORDER BY CampaignNumber asc

 

This gives me a number for each unique observation of campaignname. It is giving out numbers by when it was first observed in APPLICATIONCAMPAIGNS_SRGT_ID

Kiteulf_1-1639561947243.png

 

How do I do the same in SAS?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Using PROC SUMMARY and a DATA step:

data Applicationcampaigns;
input Campaignname $ APPLICATIONCAMPAIGNS_SRGT_ID;
datalines;
A 2
A 4
B 7
C 1
C 5
C 6
;


proc summary data=Applicationcampaigns nway;
class campaignname;
var applicationcampaigns_srgt_id;
output out=sum (drop=_type_ rename=(_freq_=NBROFCAMPAIGNS)) min()=;
run;

proc sort data=sum;
by applicationcampaigns_srgt_id;
run;

data want;
set sum;
CampaignNumber = _n_;
keep Campaignname NBROFCAMPAIGNS CampaignNumber;
run;

Note that this is based on the data I had to make up; if you want code that works with your data, post it as a data step with datalines, like I did.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

Using PROC SUMMARY and a DATA step:

data Applicationcampaigns;
input Campaignname $ APPLICATIONCAMPAIGNS_SRGT_ID;
datalines;
A 2
A 4
B 7
C 1
C 5
C 6
;


proc summary data=Applicationcampaigns nway;
class campaignname;
var applicationcampaigns_srgt_id;
output out=sum (drop=_type_ rename=(_freq_=NBROFCAMPAIGNS)) min()=;
run;

proc sort data=sum;
by applicationcampaigns_srgt_id;
run;

data want;
set sum;
CampaignNumber = _n_;
keep Campaignname NBROFCAMPAIGNS CampaignNumber;
run;

Note that this is based on the data I had to make up; if you want code that works with your data, post it as a data step with datalines, like I did.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 643 views
  • 0 likes
  • 2 in conversation