Hello ,
I am in urgent need of a SAS EG code. I need the same thing which I asked you earlier but the only thing is like now I added one more category. I have attached the excel sheet also which has input and the output which I required. First of all , I need to sort the category, category2,Month in descending order . For each category , there are four category2 level ( 1,2,3,4). So My requirement is that for each category and its each category2 level , first I need check used column . and in used column , I have to find out the number greater than or equal to 3 and need to select only the continuous rows. Suppose with Category 'A' having Category2 as '4', in "used"column the data are 12,12,2,3,8,10 , then I need to select all rows only from used column data 3 ( 3,8,10) Same thing I have highlighted in" sorted data" that whatever the data is required in output sheet. and if in some day I have one more category after category2 like Category3 and again the I need the same kind of output looking this added category also. then can you suggest me where I need to make changes. In this attached sheet , the input sheet and then after sorting the input sheet by category,category2 and months and then the output sheet ( the way I want my result). I tried my concatenating the category A and category 2 , and sorting by month and then using same code I got from Sas team but in this case it is not working.please suggest me what will be easy way to do. Will some macro useful so that in future if more variables get added like category3 , I won't stuck and can use the same code.
The question and solution of my previous question link:
Solution
Hope you get my query.
Thanks
Hi ,
Sorry I shared the same code which I got from sas team.
Now here is the code which I tried. I am attaching the new excel so that it can help you to understand my actual requirement.
A bit change with the requirement. I am trying to write down what extactly I am looking for.
In the Excel sheet , Input tab for category F2, Category 4, I would like to select the continuous 6 rows from P_mnth1 201808. The logic is like I have to first check in column " Time" for each category and its category2 where Time is equal to or greater than 3. From there I have to select 6 rows continuously but if in case Time is "." as p_count is 0 and next row having "Time column" 3 orgreater than 3, then I need to take 6 rows from where time column".". Like in Category F2, Category2 as 4 , I want rows from 082018 as P_count is 0 and next row below it have time 3. Like now for categoryF2 with category2 as 3, I will select rows from 032018.
This what modification I have from earlier query . earlier I was looking the continuous rows where time column is 3 or greater 3 starts . I was not including the row where time column is ".". for ex earlier , with same category F2, Category2 as 4 , I was looking rows from 072018.
Hope this helps in understanding as its difficult to explain in writing all.
The code over which I was trying is earlier one only so for selecting Continous 6 rows from time 3 or greater than 3 .
Request you to please look at the excel attached. The input and output which I really want.
Here is the code which I was trying .
PROC SQL;
Create Table NEW As
Select Distinct
Category,
Category2,
P_mnth1,
PCount,
Time,
(CAT(Category,Category2)) As New Category
From SAS file
Order by
Category desc,
Category desc,
P_mnth1 desc;
Quit;
PROC SQL noprint;
Create table work.WANTA As
Select
Category,
Category2,
New Category,
P_mnth1 As MONTH FORMAT= YYMMN6.
From work.New
Where Time ❤️
Group by New Category
Order by
Category DESC,
Category2 Desc,
New Category DESC,
P_mnth1 DESC;
Quit;
PROC SQL noprint;
Create table work.want_FINAL AS
Select
a.*
From work.New AS a
LEFT JOIN work.WANTA As b ON a.New category = b.New Category;
QUIT;
Waiting for your response. In excel sheet , I attached tab named as earlier output (this code is for the same only modification I did was to concatenate two coulmns and then use the above code but then it is not working) , the tab named as " New Output" is exactly what I am looking now.
Am I able to make you to understand the requirement
I'm guessing that code still isn't what you ran or has errors. Did you try and fix the errors?
I'm seeing syntax errors immediately - remember to fix the issues in the order you encounter them. If you're not getting errors, please post your full log from the code above. I can't open attachments so I can either try and look at this later tonight, if I remember or you can try and post your data and output as text/images or wait for someone else to see if they can help.
Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
@priyanka14 wrote:
Hi ,
Sorry I shared the same code which I got from sas team.
Now here is the code which I tried. I am attaching the new excel so that it can help you to understand my actual requirement.
A bit change with the requirement. I am trying to write down what extactly I am looking for.
In the Excel sheet , Input tab for category F2, Category 4, I would like to select the continuous 6 rows from P_mnth1 201808. The logic is like I have to first check in column " Time" for each category and its category2 where Time is equal to or greater than 3. From there I have to select 6 rows continuously but if in case Time is "." as p_count is 0 and next row having "Time column" 3 orgreater than 3, then I need to take 6 rows from where time column".". Like in Category F2, Category2 as 4 , I want rows from 082018 as P_count is 0 and next row below it have time 3. Like now for categoryF2 with category2 as 3, I will select rows from 032018.
This what modification I have from earlier query . earlier I was looking the continuous rows where time column is 3 or greater 3 starts . I was not including the row where time column is ".". for ex earlier , with same category F2, Category2 as 4 , I was looking rows from 072018.
Hope this helps in understanding as its difficult to explain in writing all.
The code over which I was trying is earlier one only so for selecting Continous 6 rows from time 3 or greater than 3 .
Request you to please look at the excel attached. The input and output which I really want.
Here is the code which I was trying .
PROC SQL;
Create Table NEW As
Select Distinct
Category,
Category2,
P_mnth1,
PCount,
Time,
(CAT(Category,Category2)) As New Category
From SAS file
Order by
Category desc,
Category desc,
P_mnth1 desc;
Quit;
PROC SQL noprint;
Create table work.WANTA As
Select
Category,
Category2,
New Category,
P_mnth1 As MONTH FORMAT= YYMMN6.
From work.New
Where Time ❤️
Group by New Category
Order by
Category DESC,
Category2 Desc,
New Category DESC,
P_mnth1 DESC;
Quit;
PROC SQL noprint;
Create table work.want_FINAL AS
Select
a.*
From work.New AS a
LEFT JOIN work.WANTA As b ON a.New category = b.New Category;
QUIT;
Waiting for your response. In excel sheet , I attached tab named as earlier output (this code is for the same only modification I did was to concatenate two coulmns and then use the above code but then it is not working) , the tab named as " New Output" is exactly what I am looking now.
Am I able to make you to understand the requirement
I am attaching the snapshot of input and the output which I required. I am sharing the sample data .
Input
If we combine this all snaps , it will be the input . For F4 Category , Category2 are 1,2,3,4 , Then F8 category , having category2 as 4,3. You can arrange the snaps as I am not able to do that so that all F4 category comes first then all F8 category. The highlighted one are what I want in my output
The output which I am looking :
Continous of output
Hope this will help. I am not able to share the required input.
Thanks
Priyanka
Sorry , I know I am not able explain my requirement properly or share the files . But just hoping that through snaps it will somewhat clear that what I am looking for
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.